ASP MVC3 FileResult与口音+ IE8 - 窃听? [英] ASP MVC3 FileResult with accents + IE8 - bugged?

查看:258
本文介绍了ASP MVC3 FileResult与口音+ IE8 - 窃听?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文件名包含重音,它按预期工作在Opera,FF,Chrome和IE9。

但在IE8的文件类型是未知文件类型,并显示文件作为文件名(实际上是URL的最后一部分)。

有谁知道一个解决方法吗?除了在文件名称取代特字?

测试code:(文件|新项目|添加控制器)

 公共类FileController:控制器
{
    公众的ActionResult指数(布尔?口音)
    {
        字节[] =内容新的字节[] {1,2,3,4};        返回文件(内容,应用程序/八位字节流,true.Equals(口音)dsaé.txt?:dsae.txt);
    }
}

测试它是这样的:
的http://本地主机/文件
的http://本地主机/文件口音=真

编辑=>该办法对我来说,如果有兴趣的人:

 公共类FileContentResultStupidIE:// FileContentResult是啊,也许我没有完全政治正确,但仍...
{
    公共FileContentResultStupidIE(字节[] fileContents,字符串的contentType):基地(fileContents,则contentType){}    公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
    {
        变种B = context.HttpContext.Request.Browser;
        如果(B = NULL&放大器;!&安培; b.Browser.Equals(IE,StringComparison.OrdinalIgnoreCase)及和放大器; b.MajorVersion< = 8)
        {
            context.HttpContext.Response.AppendHeader(内容处置,附件;文件名= \\+ HttpUtility.UrlPathEn code(base.FileDownloadName)+\\);
            WriteFile的(context.HttpContext.Response);
        }
        其他
        {
            base.ExecuteResult(上下文);
        }
    }
}


解决方案

尝试添加您的控制器动作内以下行:

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

您可以看一看的以下博客帖子,其中讨论了这些问题。遗憾的是没有一个通用的解决方案,将所有的浏览器中运行。

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);
        }
    }
}

解决方案

Try adding the following line inside your controller action:

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

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.

这篇关于ASP MVC3 FileResult与口音+ IE8 - 窃听?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆