douchengjue9892 2014-02-11 16:34
浏览 49
已采纳

Chrome的HTTP标头

I'm attempting to allow the user to download a file, and it is working perfectly in Firefox. My problem becomes that in Chrome, it doesn't seem to accept the file name that I give in the header, it simply ignores it and creates something of its own, and I'm noticing that its sending double headers, which are identical.

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 

The file that's generated from the above is Filename_3_from_2014_02_11_11_25_19_Custom.csv-, attachment which is distinctly different than what the header is telling it to get.

The code that sends the headers is below:

// 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));

Am I missing a header that Chrome demands, or do I need to subtly alter one of these headers to make it work as expected? I've tried application/force-download, application/vnd.ms-excel as its a CSV intended for Excel use, but none of them seem to work.

I tried things from this question: HTTP Headers for File Downloads but nothing seemed to work.

  • 写回答

2条回答 默认 最新

  • dongwei5794 2014-02-11 19:39
    关注

    After much playing around with this issue, it turns out that the problem was due to me sending the headers early, and then also returning my response object. Due to this, the headers were being sent twice, which Firefox appears to be perfectly content with, but Chrome becomes very confused and changes the file name to indicate an issue has arisen, but still properly allows the user to download the file.

    Basically, I had to remove the line that said $response->sendHeaders(); and simply return the object, thus resolving the issue with double headers, and the file name not formatting properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境