简体   繁体   English

Chrome的HTTP标头

[英]HTTP Headers for Chrome

I'm attempting to allow the user to download a file, and it is working perfectly in Firefox. 我试图允许用户下载文件,并且在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. 我的问题是,在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 

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. 从上面生成的文件是Filename_3_from_2014_02_11_11_25_19_Custom.csv-, attachment与标头告诉其获取的Filename_3_from_2014_02_11_11_25_19_Custom.csv-, attachment明显不同。

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? 我是否缺少Chrome要求的标头,还是需要巧妙地更改其中一个标头以使其按预期工作? 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. 我已经尝试将application / force-download,application / vnd.ms-excel作为CSV格式供Excel使用,但它们似乎都不起作用。

I tried things from this question: HTTP Headers for File Downloads but nothing seemed to work. 我从以下问题尝试了一些方法: 文件下载的HTTP标头,但似乎没有任何效果。

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. 因此,标头被发送了两次,Firefox似乎很满意,但Chrome变得非常混乱,并更改了文件名以指示出现问题,但仍允许用户下载文件。

Basically, I had to remove the line that said $response->sendHeaders(); 基本上,我必须删除表示$response->sendHeaders(); and simply return the object, thus resolving the issue with double headers, and the file name not formatting properly. 并简单地返回该对象,从而解决了双标头问题,并且文件名格式不正确。

Try 尝试

<?php

use Symfony\Component\HttpFoundation\Response;

//...

$file = '/path/of/your/file';

return new Response(file_get_contents($file), 200, array(
    'Content-Disposition' => 'inline; filename="'.$file.'"'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM