2012-03-28 111 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