1

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.

Community
  • 1
  • 1
Gyhth
  • 1,155
  • 9
  • 21

2 Answers2

3

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.

Gyhth
  • 1,155
  • 9
  • 21
  • 1
    I actually love you, just spent hours of my life trying to debug why duplicate headers were being returned. My ->before() event was calling return $response->send() rather than just return $response; Thanks again! – Gavin Gilmour Jul 14 '15 at 12:16
  • 1
    I was wondering why: $response->headers->set('Access-Control-Allow-Origin', '*'); $response->sendHeaders(); Gave me: "The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed." in Chrome. Thanks for solving this for me! – okdewit Dec 02 '15 at 13:54
1

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.'"'
));
Vincent Barrault
  • 2,906
  • 1
  • 19
  • 17
  • It's not a file that actually exists, but rather it's data that is collected on the fly. Actually, could that be it? Should I be using inline disposition instead of attachment? – Gyhth Feb 11 '14 at 17:04
  • file_get_contents (http://fr.php.net/file_get_contents) return a string. I use this way for any kind of file – Vincent Barrault Feb 11 '14 at 17:10
  • It already get the data as a string, as it's something I build using information from the database. I'll try inline though and see if that'll achieve what I need. Update: Same problem even when changing to inline, instead now it's .csv-, inline at the end of the file name. – Gyhth Feb 11 '14 at 17:14