Reputation: 115
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
Upvotes: 1
Views: 7213
Reputation: 51
Putting
Response.Clear();
Response.ClearHeaders();
Before
Response.Charset = encoding.WebName;
solved this for me
Upvotes: 0
Reputation: 71
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);
Upvotes: 7