public ActionResult GetExcelFile() {
var fileDownloadName = "File.xlsx";
var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var package = new ExcelPackage();
var fileStream = new MemoryStream();
package.SaveAs(fileStream);
fileStream.Position = 0;
return File(fileStream, contentType, fileDownloadName);
}
But, unfortunately, in Chrome the name of the returned file is GetExcelFile.xlsx.
And in IE9 the file name is something like this =_utf-8_B_0JjQtNC10L3RgtC40YTQuNC60LDRhtC40Y8g0KHQndCY0JvQoS7Qn9C+_=%0d%0a =_utf-8_B_0LvQvdGL0Lkg0YEg0L7RiNC40LHQutCw0LzQuC54bHN4_=.
What should I do to make file downloadable with valid file name in all browsers?
Dima Kurguzo...
0 Points
3 Posts
Download file on button click with calling Controller's action
Nov 29, 2012 09:20 AM|LINK
Hi, I am new to ASP.NET MVC. So I ask You to help me.
I have an idea to make a button. I can click it and download a file.
<a href="@Url.Action("GetExcelFile", "Download")"> Download <img src="@Url.Content("~/Content/excel_icon.png")" title= "Download Excel" /> </a>And Controller's action code.
public ActionResult GetExcelFile() { var fileDownloadName = "File.xlsx"; var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; var package = new ExcelPackage(); var fileStream = new MemoryStream(); package.SaveAs(fileStream); fileStream.Position = 0; return File(fileStream, contentType, fileDownloadName); }But, unfortunately, in Chrome the name of the returned file is GetExcelFile.xlsx.
And in IE9 the file name is something like this =_utf-8_B_0JjQtNC10L3RgtC40YTQuNC60LDRhtC40Y8g0KHQndCY0JvQoS7Qn9C+_=%0d%0a =_utf-8_B_0LvQvdGL0Lkg0YEg0L7RiNC40LHQutCw0LzQuC54bHN4_=.
What should I do to make file downloadable with valid file name in all browsers?
ignatandrei
All-Star
134929 Points
21622 Posts
Moderator
MVP
Re: Download file on button click with calling Controller's action
Nov 29, 2012 09:26 AM|LINK
FileStreamResult fsr=new FileStreamResult (fileStream ,contentType)
fsr.FileDownloadName = fileDownloadName;
return fsr;
Dima Kurguzo...
0 Points
3 Posts
Re: Download file on button click with calling Controller's action
Nov 29, 2012 09:36 AM|LINK
No, it is not a cure.
is the same. It returns new FileStreamResult without setting properties or using of object initializer, but it is just a shorter and cleaner code.
Dima Kurguzo...
0 Points
3 Posts
Re: Download file on button click with calling Controller's action
Dec 24, 2012 09:31 AM|LINK
Oh, several weeks ago I found a solution. It would be wise to post it here.
I should change the URL called by the link - it must end with a filename (just like if we pass a filename as an argument to a controller's action).
If this is done then a browser will suggest you to download 'FileDownloadName.xlsx'