加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 带有重音IE8的ASP MVC3 FileResult – 被窃听

发布时间:2020-12-16 03:21:00 所属栏目:asp.Net 来源:网络整理
导读:如果文件名包含重音符号,则它在Opera,FF,Chrome和IE9中按预期工作. 但在IE8文件类型中是“未知文件类型”,并显示“文件”作为文件名(实际上是URL的最后一部分). 有没有人知道解决方法?替换文件名中的“特殊”字符除外? 测试代码:(文件|新项目|添加控制器)
如果文件名包含重音符号,则它在Opera,FF,Chrome和IE9中按预期工作.

但在IE8文件类型中是“未知文件类型”,并显示“文件”作为文件名(实际上是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);
        }
    }
}

解决方法

尝试在控制器操作中添加以下行:

Response.HeaderEncoding = Encoding.GetEncoding("iso-8859-1");

您可以查看讨论这些问题的following blog post.不幸的是,没有一个通用的解决方案适用于所有浏览器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读