Reputation: 18534
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?
Upvotes: 2
Views: 2282
Reputation: 18534
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.
Upvotes: 2
Reputation: 718
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).
Upvotes: 0