Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to force download a file over AJAX in Zend Framework?
    text
    copied!<p>I am looking for a way to show the browser's download dialog page when a user clicks on the download button.</p> <p>This is my HTML - </p> <pre><code> &lt;span id="ajaxdownloadcontent" class="ajaxaction ajaxbutton" onclick="javascript:AjaxDownloadContent('http://localhost/ajax/download/pic/12')"&gt; Download &lt;/span&gt; </code></pre> <p>My Javascript - </p> <pre><code>function AjaxDownloadContent(path) { $.post(path); } </code></pre> <p>My controller, AjaxController.php - </p> <p> <pre><code>class AjaxController extends Zend_Controller_Action { public function init() { if ($this-&gt;getRequest()-&gt;isXmlHttpRequest()) { $this-&gt;_helper-&gt;layout-&gt;disableLayout(); $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(TRUE); } } public function downloadAction() { if ($this-&gt;getRequest()-&gt;isXmlHttpRequest()) { $this-&gt;getResponse() -&gt;clearAllHeaders() -&gt;setHeader('Content-Disposition', 'attachment;filename="Google_Logo.gif"') -&gt;sendHeaders() -&gt;setBody(file_get_contents("http://www.google.com/logos/logo.gif")) -&gt;sendResponse(); return true; } </code></pre> <p>This is how the headers look like in firebug (Note that content type has been changed to text/html)</p> <p><img src="https://i.stack.imgur.com/CT38x.png" alt="alt text"></p> <p>I think this is because of the following code in the bootstrap.</p> <pre><code>public static function sendResponse(Zend_Controller_Response_Http $response) { if (!headers_sent ()) { $response-&gt;setHeader('Content-Type', 'text/html; charset=UTF-8', true); } $response-&gt;sendResponse(); } </code></pre> <p>And finally the HTML response looks like something below - </p> <p><img src="https://i.stack.imgur.com/pMm52.png" alt="alt text"></p> <p><strong>How to force download the file(image in this case) over AJAX when the user clicks the download button?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload