2012-03-28 118 views
1

我試圖通過操作傳送CSV文件。響應中的MIME類型仍然顯示爲text/html。有人可以幫忙嗎?謝謝發送附件/從Symfony動作下載文件

 //$this->setLayout(false); 
     //$this->getUser()->shutdown(); 
     //sfConfig::set('sf_web_debug', false); 
     $response = $this->getContext()->getResponse(); 
     $response = $this->getResponse(); 
     $response->clearHttpHeaders(); 
     $response->setHttpheader('Pragma: public', true); 
     $response->addCacheControlHttpHeader('Cache-Control', 'must-revalidate'); 
     $response->setContentType('application/octet-stream', true); 
     $response->setHttpHeader('Content-Description', 'File Transfer'); 
     $response->setHttpHeader('Content-Transfer-Encoding', 'binary', true); 
     $response->setHttpHeader('Content-Length', filesize($file_path)); 
     $response->setHttpHeader('Content-Disposition', 'attachment; filename="' . $file_name . '"'); 
     $this->getResponse()->sendHttpHeaders(); 
     //$response->setContent(file_get_contents($file_path)); 
     //readfile($file_path); 
     $this->renderText(file_get_contents($file_path)); 
     //return sfView::NONE; 

     return sfView::HEADER_ONLY; 

相信我,在到這裏之前,我已經把所有搜索結果都變成了紫色。正如你可以看到所有的組合&以上組合,我仍然無法得到這個工作!

+0

這是得太做作一點 - 不應該需要HEADER_ONLY和sendHttpHeaders,但其餘的是如何像靜態文件服務器一樣提供良好的服務概覽。 – aredridel 2012-07-28 15:56:55

回答

6

嘗試這樣的:

$path = 'absolute/path/to/the/file'; 

/** @var $response sfWebResponse */ 
$response = $this->getResponse(); 

$response->setContentType('text/csv'); 
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($path) . '"'); 
$response->setContent(file_get_contents($path)); 

return sfView::NONE; 

注:文件名必須是ASCII看到RFC 6266

+0

謝謝,幾天後會試試這個! – Prasad 2012-04-17 13:17:45