【问题标题】:HTTP Headers for ChromeChrome 的 HTTP 标头
【发布时间】: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


    【解决方案1】:

    在解决了这个问题之后,事实证明问题是由于我提前发送了标头,然后还返回了我的响应对象。因此,标头被发送了两次,Firefox 似乎对此非常满意,但 Chrome 变得非常困惑并更改文件名以表明出现问题,但仍允许用户下载文件。

    基本上,我必须删除表示$response-&gt;sendHeaders(); 的行并简单地返回对象,从而解决了双标题的问题,并且文件名格式不正确。

    【讨论】:

    • 我真的很爱你,只是花了我生命中的几个小时试图调试为什么会返回重复的标题。我的 ->before() 事件正在调用 return $response->send() 而不仅仅是 return $response;再次感谢!
    • 我想知道为什么:$response->headers->set('Access-Control-Allow-Origin', ''); $response->sendHeaders();给我:“'Access-Control-Allow-Origin' 标头包含多个值 '、*',但只允许一个。”在铬。感谢您为我解决这个问题!
    【解决方案2】:

    试试

    <?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.'"'
    ));
    

    【讨论】:

    • 它不是实际存在的文件,而是动态收集的数据。其实,可能是这样吗?我应该使用内联处置而不是附件吗?
    • file_get_contents (fr.php.net/file_get_contents) 返回一个字符串。我对任何类型的文件都使用这种方式
    • 它已经将数据作为字符串获取,因为它是我使用数据库中的信息构建的。我会尝试内联,看看是否能达到我的需要。更新:即使更改为内联,也会出现同样的问题,而不是现在它是 .csv-,内联在文件名的末尾。
    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 2011-05-24
    • 2011-12-03
    • 2012-07-18
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多