ASP.NET MVC FileResult with specific Filename and Safari or Chrome
I'm having problems with FileResult returning a file with a specific filename. In the database the filename is just and ID + extension (开发者_如何学运维e.g: 456789.mp3)
This piece of code
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/octet-stream", "myfile.mp3");
Work well in every browser except Webkit browsers (Chrome, Safari). Chrome and Safari receive files as original filename (456789.mp3). When I add headers with
Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.mp3");
Safari receives the file as myfile.mp3attach (notice "attach" appended to the extension?), however Chrome receives this file as myfile.mp3,attach (it appends ",attach" to the extension)
Has anyone experienced this kind of a problem?
Thanks
It has helped me:
var encoding = System.Text.Encoding.UTF8;
Response.Charset = encoding.WebName;
Response.HeaderEncoding = encoding;
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", (Request.Browser.Browser == "IE") ? HttpUtility.UrlEncode(fileName, encoding) : fileName));
return File(fs, contentType, fileName);
Putting
Response.Clear();
Response.ClearHeaders();
Before
Response.Charset = encoding.WebName;
solved this for me
精彩评论