Friday, August 13, 2010

Download Files via Action in Symfony

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();  
   readfile(sfConfig::get('sf_upload_dir').'/'.$request->getParameter('directory').'/'.$request->getParameter('filename'));
   return sfView::NONE;
}
This action requires a 'directory' and a 'filename' parameter.

No comments:

Post a Comment