簡體   English   中英

Chrome的HTTP標頭

[英]HTTP Headers for Chrome

我試圖允許用戶下載文件,並且在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與標頭告訴其獲取的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作為CSV格式供Excel使用,但它們似乎都不起作用。

我從以下問題嘗試了一些方法: 文件下載的HTTP標頭,但似乎沒有任何效果。

經過大量處理后,事實證明,問題是由於我提早發送了標頭,然后又返回了響應對象。 因此,標頭被發送了兩次,Firefox似乎很滿意,但Chrome變得非常混亂,並更改了文件名以指示出現問題,但仍允許用戶下載文件。

基本上,我必須刪除表示$response->sendHeaders(); 並簡單地返回該對象,從而解決了雙標頭問題,並且文件名格式不正確。

嘗試

<?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