ASP MVC3 FileResult 带有重音符号 + IE8 - 被窃听了吗?
如果文件名包含重音符号,则它在 Opera、FF、Chrome 和 IE9 中按预期工作。
但在 IE8 中文件类型是“未知文件类型”,并显示“file”作为文件名(实际上是 URL 的最后部分)。
有人知道解决方法吗?除了替换文件名中的“特殊”字符之外?
测试代码:(文件|新项目|添加控制器)
public class FileController : Controller
{
public ActionResult Index(bool? Accents)
{
byte[] content = new byte[] { 1, 2, 3, 4 };
return File(content, "application/octet-stream", true.Equals(Accents) ? "dsaé.txt" : "dsae.txt");
}
}
这样测试: http://localhost/file,以及 http://localhost/file?accents=true
编辑 =>如果有人感兴趣的话,我的“解决方案”是:
public class FileContentResultStupidIE : FileContentResult //yeah, maybe i am not totally "politically correct", but still...
{
public FileContentResultStupidIE(byte[] fileContents, string contentType) : base(fileContents, contentType) { }
public override void ExecuteResult(ControllerContext context)
{
var b = context.HttpContext.Request.Browser;
if (b != null && b.Browser.Equals("ie", StringComparison.OrdinalIgnoreCase) && b.MajorVersion <= 8)
{
context.HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(base.FileDownloadName) + "\"");
WriteFile(context.HttpContext.Response);
}
else
{
base.ExecuteResult(context);
}
}
}
If the file name contains accents, it works as expected in Opera, FF, Chrome and IE9.
But in IE8 file type is "unknown file type", and shows "file" as the file name (actually the last part of the URL).
Does anyone know a workaround? Other than replacing the "special" characters in the file name?
The test code: (file | new project | add controller)
public class FileController : Controller
{
public ActionResult Index(bool? Accents)
{
byte[] content = new byte[] { 1, 2, 3, 4 };
return File(content, "application/octet-stream", true.Equals(Accents) ? "dsaé.txt" : "dsae.txt");
}
}
test it like this:
http://localhost/file, and
http://localhost/file?accents=true
Edit => The "solution" for me, if anyone interested:
public class FileContentResultStupidIE : FileContentResult //yeah, maybe i am not totally "politically correct", but still...
{
public FileContentResultStupidIE(byte[] fileContents, string contentType) : base(fileContents, contentType) { }
public override void ExecuteResult(ControllerContext context)
{
var b = context.HttpContext.Request.Browser;
if (b != null && b.Browser.Equals("ie", StringComparison.OrdinalIgnoreCase) && b.MajorVersion <= 8)
{
context.HttpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(base.FileDownloadName) + "\"");
WriteFile(context.HttpContext.Response);
}
else
{
base.ExecuteResult(context);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在控制器操作中添加以下行:
您可以查看以下博客文章 讨论了这些问题。不幸的是,没有一个适用于所有浏览器的通用解决方案。
Try adding the following line inside your controller action:
You may take a look at the following blog post which discusses those issues. Unfortunately there isn't a general solution which will work among all browsers.
这是用户在某个时刻上传到您的系统的文件吗?如果是这样,请限制在文件名中使用重音符号。如果不是 - 不要在文件名中使用重音符号:)。
Is this a file the user is uploading to your system at some point? If so, restrict the use of accents in a file name. If not - don't use accents in your file names :).