I am trying to make a download action that downloads a Word doc generated in the 'download' controller using PHPDOCX. So far PHPDOCX is able to save the desired .docx file in the correct folder, but something goes wrong when I try to download it. Since Media Views were deprecated, I must use the CakeResponse file method as suggested in the CakePHP 2.x Cookbook:
// In the controller:
$this->response->file($file['path'], array('download' => true, 'name' => $filename));
The character encoding declaration of the HTML document was not found
when prescanning the first 1024 bytes of the file. When viewed in a
differently-configured browser, this page will reload automatically.
The encoding declaration needs to be moved to be within the first 1024
bytes of the file.
The requested file APP/files/info_sheets/filename.docx was not found or not readable
Headers are not sent whenis called either.
They are just buffered until the response is actually sent.
First of all, you might try to disable autoRender, otherwise CakePHP might still try to render your view and layout;
$this->autoRender = false;
Also, haven't tested it, but have you tried this to set the header:
// register the type $this->response->type(array('docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')); // set the type for the response $this->response->type('docx');
See the documentation: http://book.cakephp.org/2.0/en/controllers/request-response.html#dealing-with-content-types