Firefox and Content-Disposition header

Garrett R picture Garrett R · Feb 6, 2012 · Viewed 11.9k times · Source

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.

Answer

Asad Saeeduddin picture Asad Saeeduddin · Nov 27, 2012

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.