HTTP Headers for Chrome

I am trying to allow a user to upload a file and it works fine in Firefox. My problem is that in Chrome it does not seem to accept the file name that I give in the header, it just ignores it and creates something of its own, and I notice that its sending double headers 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 

A file created from the above is Filename_3_from_2014_02_11_11_25_19_Custom.csv-, attachment , which is different from what the header tells it to receive.

The code sending 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 the title Chrome needs, or do I need to subtly change one of these titles to make it work properly? I tried the application / force -download, application / vnd.ms-excel as my CSV, intended for use in Excel, but none of them work.

I tried things from this question: HTTP headers for uploading files , but nothing worked.

+1
google-chrome php symfony
source share
2 answers

After a long game with this problem, it turns out that the problem is that I send the headers earlier, and then return the response object. Because of this, the headers were sent twice, and Firefox seemed completely satisfactory, but Chrome got very confused and changed the file name to indicate that the problem was occurring, but still correctly allows the user to download the file.

Basically, I had to delete the line indicated by $response->sendHeaders(); , and simply return the object, thereby resolving the problem with double headers, and the file name will not be formatted correctly.

+3
source

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.'"' )); 
+1
source

All Articles