Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox and Content-Disposition header

I have a problem with an attachment's name. When I call the site on google chrome it returns the file with the right name and extension. I tested it with internet explorer and it works fine too. The issue lies with only Firefox. I call the site and it returns the first word on the file title and no extension.

For example if I wanted a file called "My report.docx" it turns a file called "My". I Googled around and it turns out this is a common issue with people because browsers read the headers differently. They said the fix is to quote the file name:

Content-Disposition: attachment; filename=My Report.docx

is now: (note the quotes)

Content-Disposition: attachment; filename="My Report.docx"

However, that did not work for me.

On chrome it returned "My Report.docx" (actually with the quotes). Firefox returned a odd file that had the proper extension and proper name and no quotes yet it could not be executed. It was the proper file size, proper extension, and proper name yet it could not be executed. Also it returns a space before and after the file name.

like image 854
Garrett R Avatar asked Feb 06 '12 00:02

Garrett R


Video Answer


2 Answers

I know this is a very old question, but I was recently having the same problem. The solution is to either

  1. Encode your filename per RFC2184 or,
  2. If you don't have special characters in your filename, quote it in the content disposition string.

Since you've already tried 2, you could try using 1 and seeing how that works out.

Usually I use the ContentDisposition class to generate my header for me:

Dim contentDispositionHeader = New Net.Mime.ContentDisposition() With {.FileName = filename}
Response.AddHeader("Content-Disposition", contentDispositionHeader.ToString())

Hope this helps.

like image 178
Asad Saeeduddin Avatar answered Oct 09 '22 19:10

Asad Saeeduddin


This should work as expected, here's another SOq with the same problem:

  • Downloading a file with a different name to the stored name

and also the Mozilla page (I guess you were referencing this one):

  • http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download

I don't know the specifics of your server side code, but here are some things to confirm / try:

  • If you have PHP available at the server, can you try the code from the first link above? If not, you can probably find something on the Net in your language of choice. That way, you can confirm whether the issue is in your code or somewhere else (server setup, browser, etc.)
  • Is this happening on other client machines (i.e. where you try the download from) or only on that one? You might want to try others to confirm.
  • Is this working fine in IE / Safari or some other browser? You can even try doing it with wget or curl from the command line or something like that.
  • Are you also providing the Content-Type header correctly?
  • Can you try downloading some other file or a file of a different type, e.g. a .png or a .xls? In fact, probably the easiest would be to try a plain text file (text/plain) and then take it from there.

Hope this helps.

like image 30
icyrock.com Avatar answered Oct 09 '22 18:10

icyrock.com