9

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.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Garrett R
  • 521
  • 3
  • 6
  • 21
  • 3
    possible duplicate of [How to encode the filename parameter of Content-Disposition header in HTTP?](http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http) – Asad Saeeduddin Oct 14 '13 at 18:22
  • 1
    If you're wondering why your Firefox is ignoring this header, check if you have the `InlineDisposition` addon installed: it's a great addon written specifically to disable `Content-Disposition` forcing a download, and is easy to forget about when doing web development. – Roman Starkov Sep 25 '14 at 13:13
  • 2
    Double quotes worked in IE, Chrome and FireFox for me, using latest versions. – angularsen Jun 10 '15 at 11:15

2 Answers2

5

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.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
4

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

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

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.

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • Alright thanks for the reply, but that first link is what I am doing in the php code. And yes that is the thing I'm referring too. I tried to do what it said by quoting it but all that it did was place extra spaces on the front and end on the file name so it would be like " My report.docx " and that's why it is not being able to be executed. I tried using str_replace() but that didn't work. I tried everything. I can get it to work for one browser but than it break on the other... its almost impossible to find a balance... I also am not providing a Content-Type header – Garrett R Feb 06 '12 at 02:08
  • Alright so I figured out what it use with all the damn grief. It's because I'm using a variable as a name. My codes written: $name2 = "filename="."$name"; header('Content-Disposition: attachment; '.$name2); It treats the "$" in the variable as a space when it's processed by the server. I got to figure out a way to by pass that.... I have no idea if thats even possible -.- – Garrett R Feb 06 '12 at 02:18