我正在使用 Symfony 2 制作一个“下载”控制器,其唯一目的是发送标头,以便我可以强制下载 .csv 文件,但它无法正常工作。
$response = new Response();
$response->headers->set('Content-Type', "text/csv");
$response->headers->set('Content-Disposition', 'attachment; filename="'.$fileName.'"');
$response->headers->set('Pragma', "no-cache");
$response->headers->set('Expires', "0");
$response->headers->set('Content-Transfer-Encoding', "binary");
$response->headers->set('Content-Length', filesize($fileName));
$response->prepare();
$response->sendHeaders();
$response->setContent(readfile($fileName));
$response->sendContent();
$fileName
is a "info.csv"
细绳。这就是我在控制器内的操作,没有返回语句。当我尝试返回时Response
对象,文件的内容显示在浏览器中,不是我想要的结果。
我发现的问题是,在某些页面中我确实得到了我的info.csv file
,但在其他人中我得到的只是一条消息:
No webpage was found for the web address: http://mywebpage.com/download
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
我完全确定该文件存在,所以肯定还有其他问题。另外,routing.yml 工作正常,因为我确实从也链接到该路径的其他页面获取该文件。
Apache 错误日志没有显示任何相关内容。
以前有人在 Symfony 2 上强制下载 .csv 文件吗?如果是这样,我做错了什么?