Content-Disposition 标头中的 Unicode [英] Unicode in Content-Disposition header

查看:26
本文介绍了Content-Disposition 标头中的 Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HttpHandler child 中实现的 HttpContext 对象来下载文件,当我在文件名中包含非 ascii 字符时,它在 IE 中看起来很奇怪,而在 Firefox 中看起来很好.

I am using HttpContext object implemented in HttpHandler child to download a file, when I have non-ascii characters in file name it looks weird in IE whereas it looks fine in Firefox.

下面是代码:-

       context.Response.ContentType = ".cs";
context.Response.AppendHeader("Content-Length", data.Length.ToString());
context.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}",filename));
        context.Response.OutputStream.Write(data, 0, data.Length);

context.Response.Flush();

当我在文件名字段中提供 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó' 时,它看起来与我的不同在文件名中有它在Firefox中看起来很好.添加 EncodingType 和 charset 已经没有用了.

when I supply 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó' in file name field it looks different than what I have in file name it looks fine in firefox. adding EncodingType and charset has been of no use.

在ie中是'ß''ä''ö''ü''ó''ß''ä''ö''ü'_'ó' 在 Firefox 中是 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó'.

In ie it is 'ß''ä''ö''ü''ó''ß''ä''ö''ü'_'ó' and in firefox it is 'ß' 'ä' 'ö' 'ü' 'ó' 'ß' 'ä' 'ö' 'ü' 'ó'.

知道如何解决这个问题吗?

Any Idea how this can be fixed?

推荐答案

我也遇到了类似的问题.您必须使用 HttpUtility.UrlEncodeServer.UrlEncode 对文件名进行编码.我还记得Firefox不需要它.此外,它在 url 编码时破坏了文件名.我的代码:

I had similar problem. You have to use HttpUtility.UrlEncode or Server.UrlEncode to encode filename. Also I remember firefox didn't need it. Moreoverit ruined filename when it's url-encoded. My code:

// IE needs url encoding, FF doesn't support it, Google Chrome doesn't care
if (Request.Browser.IsBrowser ("IE"))
{
    fileName = Server.UrlEncode(fileName);
}

Response.Clear ();
Response.AddHeader ("content-disposition", String.Format ("attachment;filename="{0}"", fileName));
Response.AddHeader ("Content-Length", data.Length.ToString (CultureInfo.InvariantCulture));
Response.ContentType = mimeType;
Response.BinaryWrite(data);

编辑

我已经更仔细地阅读了规范.首先 RFC2183 声明:

I have read specification more carefully. First of all RFC2183 states that:

当前 [RFC 2045] 语法将参数值(以及内容处置文件名)限制为 US-ASCII.

Current [RFC 2045] grammar restricts parameter values (and hence Content-Disposition filenames) to US-ASCII.

但后来我发现 [RFC 2045] 已过时,必须参考 RFC 2231,其中指出:

But then I found references that [RFC 2045] is absolete and one must reference RFC 2231, which states:

星号(*")被重复使用以提供语言和指标存在字符集信息并且正在使用编码.一个引号(")用于分隔字符集和语言信息在参数的开头价值.百分号(%")用作编码标志,它同意RFC 2047.

Asterisks ("*") are reused to provide the indicator that language and character set information is present and encoding is being used. A single quote ("'") is used to delimit the character set and language information at the beginning of the parameter value. Percent signs ("%") are used as the encoding flag, which agrees with RFC 2047.

这意味着您可以将 UrlEncode 用于非 ascii 符号,只要您包含 rfc.这是一个例子:

Which means that you can use UrlEncode for non-ascii symbols, as long as you include the encoding as stated in the rfc. Here is an example:

string.Format("attachment; filename="{0}"; filename*=UTF-8''{0}", Server.UrlEncode(fileName, Encoding.UTF8));

请注意,除了 filename* 之外,还包含 filename 以实现向后兼容性.您也可以选择其他编码并相应地修改参数,但 UTF-8 涵盖了所有内容.

Note that filename is included in addition to filename* for backwards compatibility. You can also choose another encoding and modify the parameter accordingly, but UTF-8 covers everything.

这篇关于Content-Disposition 标头中的 Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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