我正在尝试为我的用户生成并返回文件。某些文件名包含unicode字符:aäa.pdf。当尝试在IE 9中下载它们时,文件名被破坏,下载如下所示:
在Chrome中,它按预期工作。这是退货声明:
return File(fileStream: stream,
contentType: System.Net.Mime.MediaTypeNames.Application.Octet,
fileDownloadName: myModel.Name + "." + pub.OutputFileType);
如何解决IE 9的问题?
答案 0 :(得分:2)
似乎这对IE9来说根本不可行,因为评论中的namesv状态。我最终通过滥用浏览器功能解决了这个问题:当它请求类似/foo/fileÄÖ.pdf
的内容并返回文件时,会使用URL中的“文件名”。
所以,我创建了一条新路线,将我的动态下载定位到它并在动作中执行了此操作:
if (Request.Browser.Browser == "IE" && Request.Browser.MajorVersion == 9)
{
// The filename comes from the Route used, which is like host/Print/File.pdf
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment");
Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;
Response.Charset = "utf-8";
Response.HeaderEncoding = UnicodeEncoding.UTF8;
Response.ContentEncoding = UnicodeEncoding.UTF8;
byte[] buffer = new byte[4096];
int count = 0;
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
{
Response.OutputStream.Write(buffer, 0, count);
Response.Flush();
}
Response.End();
return null;
}
else
{
return File(
fileStream: stream,
contentType: System.Net.Mime.MediaTypeNames.Application.Octet,
fileDownloadName: compilation.Name + "." + pub.OutputFileType);
}