7

I use Symfony2 Framework and use the following code to export an xml file:

$response->setStatusCode(200);
$response->headers->set('Content-Type', 'application/xml');
$response->headers->set('Content-Description', 'Submissions Export');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename .'"');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');

I does not matter whether I take "Content-Transfer-Encoding" or "Pragma" or all of them except "Content-Type" and "Content-Disposition" away. The result in Chrome is always: "filename-, attachment" (without the "). In Firefox this works fine.

So for instance, if I have a file called home.xml Firefox will download home.xml, whereas Chrome will give me home.xml-, attachment.

Micha
  • 523
  • 10
  • 26
  • 1
    Possible duplicate of this http://stackoverflow.com/questions/6530569/chrome-appends-hyphen-to-the-downloaded-csv-file – Akshaya Shanbhogue Nov 07 '13 at 11:45
  • That is as far as I can tell a different issue. It is a known bug that Chrome will convert \" to - (for some reason). Maybe it is already fixed. I think my issue does not concern any kind of conversion. Chrome seems to change the filename for some reason, whether I use xml, pdf or any other sort of file. – Micha Nov 07 '13 at 11:54
  • As far as I can tell, your code also has a quote at the end of the filename. i.e; - $filename .'"' . Why don't you remove quotes and check once :) – Akshaya Shanbhogue Nov 07 '13 at 11:56
  • I did (many times). No quotes, only ", \", ', \' etc. that did not solve the problem / created (interesting) new (additional) ones. – Micha Nov 07 '13 at 12:03
  • 2
    @Akshay I got it. that post you linked is not the same problem, but it helped me find the solution. the "-" indicated that indeed one of the (") was being converted, whereas the (,) indicated some sort of "additional" file. solution: throughout development it just so happened that I send the Headers twice -.- Thanks :) – Micha Nov 07 '13 at 12:26
  • 1) Get rid of the meaningless header fields (Content-Description, Content-Transfer-Encoding) 2) Obtain an HTTP trace of what actually gets on the wire (or inspect the header fields in the browser) 3) Note that simply quoting the filename will not work properly for non-ASCII characters, see http://greenbytes.de/tech/webdav/rfc6266.html for details). – Julian Reschke Nov 07 '13 at 14:08

2 Answers2

3

I know this is a pretty old discussion, but if anyone gets here looking for help with this, I determined that chrome seems to want a semicolon at the end of the filename and then it's happy. As in (VBScript):

Response.AddHeader "Content-Disposition", "attachment;filename=Something.xls;"

That fixed it for me. -Dave

dnrayzn
  • 126
  • 1
  • 5
  • Really? - That shows a "duplicate content-disposition" error and halts for me. I'm still looking for a solution to this, I filed a bug here: https://code.google.com/p/chromium/issues/detail?id=487906 – Hackeron May 14 '15 at 02:08
1

I had the same problem in ZEND because I found a lot of non working stuff I wanted to add my anwser to this post. Since it took me a while to solve it thought more users and developers would wanted to know how I solved it. I add this just before my fopen call.

header('Content-Disposition: attachment; filename='.time().'.csv');

Solved the issue for me. Before I used a contextswitched what was buggy but now it works as a charm. Do not forget to remove previous content disposition headers hence you might get a double header error.

user3806549
  • 1,428
  • 1
  • 17
  • 25