适用于Chrome的HTTP头 [英] HTTP Headers for Chrome

查看:127
本文介绍了适用于Chrome的HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户下载一个文件,并且它在Firefox中完美运行。我的问题变成了,在Chrome中,它似乎不接受我在标题中给出的文件名,它只是忽略它并创建它自己的东西,而且我注意到它发送的双重标题相同。

  HTTP / 1.1 200 OK 
日期:2014年2月11日星期二16:25:19 GMT
服务器:Apache / #.#.##(Linux操作系统)
X-Powered-By:PHP / #.#.##
Cache-Control:private
Pragma:public
Content-Description:文件传输
Content-Disposition:附件;文件名=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 :文件传输
内容处理:附件; filename =Filename_3_from_2014_02_11_11_25_19_Custom.csv
Content-Transfer-Encoding:binary
X-ChromePhp-Data:<这里有东西>
X-Debug-Token:2f50fc
连接:关闭
Content-Type:text / plain; charset = UTF-8

从上面生成的文件是 Filename_3_from_2014_02_11_11_25_19_Custom。



发送标题的代码如下所示:

/ p>

  //生成响应
$ response = new Response();
//设定标题
$回应 - >标题 - > 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需求的标题,或者是否需要巧妙地将其中一个标题更改为让它按预期工作?我已经尝试过应用程序/强制下载,应用程序/ vnd.ms-excel作为其用于Excel使用的CSV,但它们都不起作用。



我尝试了这个问题: HTTP头为文件下载,但似乎没有任何工作。

解决方案

出问题是由于我提前发送头文件,然后返回我的响应对象。由于这个原因,头文件被发送了两次,Firefox似乎完全满足于此,但Chrome变得非常困惑,并且改变了文件名以表明出现了问题,但仍允许用户下载该文件。 p>

基本上,我必须删除 $ response-> sendHeaders(); 这一行并简单地返回对象,因此解决了双头文件的问题,并且文件名没有正确格式化。


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.

解决方案

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.

这篇关于适用于Chrome的HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆