Jun 27, 2011

Symfony/Zend: Download Attach File in the Action Controller.

Synfony


public function executeDownload(sfWebRequest $request)  
{  
$response = $this->getContext()->getResponse();  
$response->clearHttpHeaders();  
$response->addCacheControlHttpHeader('Cache-control','must-revalidate, post-check=0, pre-check=0');  
$response->setContentType('application/octet-stream',TRUE);      
$response->setHttpHeader('Content-Transfer-Encoding', 'binary', TRUE);  
$response->setHttpHeader('Content-Disposition','attachment; filename='.$request->getParameter('filename'), TRUE);  
$response->sendHttpHeaders(); // Need to send the HttpHeaders to handle a large file size
readfile(readfile('xxx.mp3'));  
return sfView::NONE;  
}

* Always dont omit the line to send the headers

Zend


$this->getResponse()->setHeader("Pragma","public");
$this->getResponse()->setHeader("Expires", "0");
$this->getResponse()->setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
$this->getResponse()->setHeader("Cache-Control","private", false);
$this->getResponse()->setHeader("Content-Type","audio/x-mp3");
$this->getResponse()->setHeader("Content-Disposition","attachment; filename=\"xxx.mp3\"");
$this->getResponse()->setHeader("Content-Transfer-Encoding","binary");
$this->getResponse()->setHeader("Content-Length", filesize('xxx.mp3'));
$this->getResponse()->setHeader("Content-Description","File Transfer");
$this->getResponse()->sendHeaders(); // Need to send the HttpHeaders to handle a large file size
$this->getResponse()->setBody(readfile('xxx.mp3'));

No comments: