【发布时间】:2014-03-09 13:50:40
【问题描述】:
我正在尝试允许用户下载一个文件,它在 Firefox 中运行良好。我的问题变成了在 Chrome 中,它似乎不接受我在标题中提供的文件名,它只是忽略它并创建自己的东西,我注意到它发送的双标题是相同的。
HTTP/1.1 200 OK
Date: Tue, 11 Feb 2014 16:25:19 GMT
Server: Apache/#.#.## (Linux OS)
X-Powered-By: PHP/#.#.##
Cache-Control: private
Pragma: public
Content-Description: File Transfer
Content-Disposition: attachment; filename="Filename_3_from_2014_02_11_11_25_19_Custom.csv"
Content-Transfer-Encoding: binary
Content-Length: 9
Cache-Control: private
Pragma: public
Content-Description: File Transfer
Content-Disposition: attachment; filename="Filename_3_from_2014_02_11_11_25_19_Custom.csv"
Content-Transfer-Encoding: binary
X-ChromePhp-Data: <Stuff here>
X-Debug-Token: 2f50fc
Connection: close
Content-Type: text/plain; charset=UTF-8
从上面生成的文件是Filename_3_from_2014_02_11_11_25_19_Custom.csv-, attachment,这与标题告诉它获取的文件明显不同。
发送标头的代码如下:
// Generate response
$response = new Response();
// Set headers
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'private', false);
$response->headers->set('Content-Type', 'text/plain');
$response->headers->set('Content-Description', 'File Transfer');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
$response->headers->set('Content-Type', 'text/plain');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Content-Length', strlen($filedata));
我是否缺少 Chrome 要求的标头,还是需要巧妙地更改其中一个标头以使其按预期工作?我已尝试将 application/force-download、application/vnd.ms-excel 作为用于 Excel 的 CSV,但它们似乎都不起作用。
我尝试了这个问题:HTTP Headers for File Downloads,但似乎没有任何效果。
【问题讨论】:
标签: php google-chrome symfony http-headers