从Symfony动作发送附件/下载文件 [英] Send attachment/Download file from Symfony action
本文介绍了从Symfony动作发送附件/下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过操作传递CSV文件。响应中的MIME类型仍显示为text / html。有人可以帮忙吗?谢谢
I'm trying to deliver a CSV file through an action. The mime-type in the response still shows as text/html. Can someone help? Thanks
//$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;
相信我,在到达这里之前,我所有的搜索结果都是紫色的。正如你可以看到所有的排列&上面的组合,我还是不能得到这个工作!
Trust me, before getting here, I've all search results purple. As you can see all the permutations & combinations above, I still couldn't get this working!
推荐答案
尝试这样:
$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;
注意: filename必须是ASCII,请参阅 RFC 6266
Note: filename must be ASCII see RFC 6266
这篇关于从Symfony动作发送附件/下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文