2

I use Controller.File(Stream fileStream, String contentType, String fileDownloadName) Method to download a file from server. In my case contentType is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. I do specify some special name for the file in fileDownloadName argument. And it works pretty well in Firefox, Opera, IE9.

However, IE8 returns the name of the action instead of the name I specify. It looks like Download.xlsx, if the url is ../Report/Download.

Why is that so and what can I do about that?


According to Content-Disposition headers in .NET the overload I'm using should solve the issue. However IE8 wants the file download name to be encoded. While, when encoded with HttpUtility.UrlPathEncode for example, Firefox starts showing the file name encoded.

Is there a universal solution in the end?

horgh
  • 17,918
  • 22
  • 68
  • 123

2 Answers2

2

As soon as I failed to work out a common way to provide the file download name for at least IE8+ and Mozilla, as a temporary workaround I use the following:

Stream report = GetReport();
string filename = GetFileName();
if (Request.Browser.Browser.Equals("IE", StringComparison.CurrentCultureIgnoreCase))
    filename = HttpUtility.UrlPathEncode(filename);

return File(
    report, 
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
    filename);

The encoded filename was shown bad by Firefox; the non-encoded - by IE8. As current culture is Ru, filenames contain cyrillic symbols, which seem to be the problem for the IE8 (and probably spaces).


I am open to any suggestions, that may lead me to the correct and\or inversal (if any) approach to provide file download names in ASP.Net MVC 3. Until that I accept this poor workaround as the answer, as it at least does the job.

horgh
  • 17,918
  • 22
  • 68
  • 123
0
 Response.AppendHeader("content-disposition", "attach;filename=" + fileName);

 return File(fileStream, contentType);

Changing "attach;" to "inline;" will cause the file to open in the browser instead of the browser prompting to save the file.

NOTE: I have also experienced issue with browsers and the value of the file name. Non-ASCII characters, spaces, and punctuation can cause this header to get ignored by the browser (Downloading file with ";" or "#" in file name ruins filename).

Community
  • 1
  • 1
Jeremy Bell
  • 698
  • 5
  • 9
  • Why should I use the `File` overload you mentioned, if I can specify the file name at once without working with `Response` directly? Besides this gives an effect I do not like at all, browsers start using non-standard dialog to prompt download. As for the link, it does not give the answer either, as I do not have any "special" characters in my filename. I tested it simply with `Report.xlsx` and even this is not worked out correctly by IE8. – horgh Apr 02 '13 at 00:17
  • Rather the encoding issue (described in the linked thread) does help to resolve the issue with any IE. However Firefox starts to show the file name encoded. Neither of the approaches seem to be universal at least among IEs and Firefox. – horgh Apr 02 '13 at 00:21
  • The filename should be quoted in case it contains a space, which will throw off browsers. `filename=\"" + filename + "\""` – Michael Stum Apr 02 '13 at 01:00
  • Can you instead clean the output filename so that it does not need any URL encoding (e.g. "a-ZA-Z_-.")? – Adrian Godong Apr 02 '13 at 01:08
  • @AdrianGodong filename is in Russian; as soon as I understand, this is the problem for IE8; and definitely I will not change the language. – horgh Apr 02 '13 at 08:16
  • @MichaelStum I tried to. However this did not change anything in IE8, while in other browsers filename appeared to be surrounded with underscores – horgh Apr 02 '13 at 08:18
  • @MichaelStum Firefox, Opera, Chrome, IE9, they all work well without any encoding. Only IE8 fails when the language is Russian. If I change it to English, the problem dissapears. If I'm right, the question in the end is what the correct way to deal with Russian download filename in IE8 (I do not even consider IE earlier versions) is? – horgh Apr 02 '13 at 08:24