统一code的内容处理标头 [英] Unicode in Content-Disposition header

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

问题描述

我正在使用的HttpHandler的孩子实施HttpContext对象下载一个文件,当我在文件名,它看起来怪异的IE浏览器非ASCII字符,而它看起来罚款的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.

下面是code: -

below is the code:-

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

当我在文件名称字段中提供嗯澶'ö那张''A 3嗯澶'ö那张''A 3',它看起来比不同的是我在文件名,它看起来罚款的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.

在也就是说,它是'ß''ä''ö''ü''ó''ß''ä''ö''ü'_'ó'而在Firefox中是嗯澶'ö那张''A 3嗯澶'ö那张''A 3'。

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

任何想法如何能解决吗?

Any Idea how this can be fixed?

推荐答案

我有类似的问题。你必须使用<一个href="http://msdn.microsoft.com/en-us/library/system.web.httputility.urlen$c$c.aspx">HttpUtility.UrlEn$c$c或 Server.UrlEn code 以EN code文件名。此外,我还记得火狐并不需要它。 Moreoverit毁了文件名时,它的URL-CN codeD。我的code:

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]是absolete和一个必须参考 RFC 2231 ,其中规定

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

星号(*)被重新使用,以提供   该指标的语言和   字符集信息是present   和编码正被使用。单   引号(')用于分隔所述   字符集和语言的信息   在参数的开始   值。百分号(%)被用作   的编码标志,它同意   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.

这意味着你可以使用UrlEn code非ASCII符号,我之前说的,但你也可以添加编码描述。下面是一个例子:

Which means that you can use UrlEncode for non-ascii symbols as I said before, but you could also add encoding description. Here is an example:

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

没有尝试,但..

Didn't try it though..

这篇关于统一code的内容处理标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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