具有特定文件名和Safari或Chrome的ASP.NET MVC FileResult

Shy*_*y82 1 safari asp.net-mvc google-chrome fileresult

我遇到FileResult返回具有特定文件名的文件的问题.在数据库中,文件名只是ID +扩展名(例如:456789.mp3)

这段代码

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/octet-stream", "myfile.mp3");
Run Code Online (Sandbox Code Playgroud)

除Webkit浏览器(Chrome,Safari)外,在每个浏览器中都能正常运行.Chrome和Safari会将文件作为原始文件名(456789.mp3)接收.当我添加标题时

Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.mp3");
Run Code Online (Sandbox Code Playgroud)

Safari接收文件为myfile.mp3attach(注意"附加"附加到扩展名?),但Chrome接收此文件为myfile.mp3,附加(它附加",附加"到扩展名)

有没有人遇到过这种问题?

谢谢

小智 7

它帮助了我:

        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);
Run Code Online (Sandbox Code Playgroud)