ASP MVC3 FileResult 带有重音符号 + IE8 - 被窃听了吗?

发布于 2024-11-28 01:55:23 字数 1535 浏览 0 评论 0原文

如果文件名包含重音符号,则它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹梦到西洲 2024-12-05 01:55:23

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

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

您可以查看以下博客文章 讨论了这些问题。不幸的是,没有一个适用于所有浏览器的通用解决方案。

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.

冷弦 2024-12-05 01:55:23

这是用户在某个时刻上传到您的系统的文件吗?如果是这样,请限制在文件名中使用重音符号。如果不是 - 不要在文件名中使用重音符号:)。

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 :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文