{"id":1020,"date":"2022-08-30T15:11:03","date_gmt":"2022-08-30T15:11:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/download-a-excel-file-through-zend-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:03","modified_gmt":"2022-08-30T15:11:03","slug":"download-a-excel-file-through-zend-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/download-a-excel-file-through-zend-collection-of-common-programming-errors\/","title":{"rendered":"Download a excel file through Zend-Collection of common programming errors"},"content":{"rendered":"<p>I need to give download excel files stored on server. But I am unable to find an example for download in ZF2. I am trying for the following code<\/p>\n<pre><code>$response = new Response();\n$response-&gt;getHeaders()-&gt;addHeaders(array(\n    'Content-Type' =&gt; 'application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet',\n    'Content-Disposition' =&gt; 'attachment;filename=\"forecast_template.xlsx\"',\n    'Cache-Control' =&gt; 'max-age=0',\n));\n$response-&gt;setContent(file_get_contents($xlsx_file_name));\n<\/code><\/pre>\n<p>But it did not work. I tried setRawBody but that gave error for undefined method. Also I tried &#8220;echo $response&#8221;, But that just dump string output for headers + binary data.<\/p>\n<ol>\n<li>\n<p>Using MVC, you can start a file download by <code>short-circuiting<\/code> the <code>MvcEvent<\/code> (to do this, just return the <code>Response<\/code> instance instead of a <code>ViewModel<\/code>).<\/p>\n<p>The following example snippet works for me:<\/p>\n<pre><code>use Zend\\Http\\Headers;\n\n(...)\n\n$response = $this-&gt;getEvent()-&gt;getResponse();\n$response-&gt;setHeaders(Headers::fromString(\"Content-Type: application\/octet-stream\\r\\nContent-Length: 9\\r\\nContent-Disposition: attachment; filename=\\\"blamoo.txt\\\"\"));\n\n$response-&gt;setContent('blablabla');\n\nreturn $response;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:14:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I need to give download excel files stored on server. But I am unable to find an example for download in ZF2. I am trying for the following code $response = new Response(); $response-&gt;getHeaders()-&gt;addHeaders(array( &#8216;Content-Type&#8217; =&gt; &#8216;application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#8217;, &#8216;Content-Disposition&#8217; =&gt; &#8216;attachment;filename=&#8221;forecast_template.xlsx&#8221;&#8216;, &#8216;Cache-Control&#8217; =&gt; &#8216;max-age=0&#8217;, )); $response-&gt;setContent(file_get_contents($xlsx_file_name)); But it did not work. I tried setRawBody but that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1020","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1020"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}