2012-06-29 34 views
0

所以它的工作原理与HttpResponse对象类:HttpListenerResponse.AddHeader不喜欢非拉丁字符

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 

(我们不得不编码为IE文件名) 但现在应该HttpListener做。它适用于IE。问题是Firefox和Chrome不会像解码编码IE标头值,但HttpResponse.AddHeader不允许非拉丁字符(代码System.Net):

if ((ch == '\x007f') || ((ch < ' ') && (ch != '\t'))) 
    throw new ArgumentException(SR.GetString("net_WebHeaderInvalidControlChars"), "value"); 

我尝试使用反射来转转检查:

Type type = response.Headers.GetType(); 
PropertyInfo info = type.GetProperty("InnerCollection", 
    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic); 
NameValueCollection headers = (NameValueCollection)info.GetValue(response.Headers, null); 
headers.Add(name, value); 

没有东西上升,但该文件的名称已完全损坏。 我该怎么做才能使它工作?

+0

从技术上讲,RFC 2616通过RFC 2045通过RFC 822将Content-Disposition文件名限制为US-ASCII。在[RFC 6266](http://tools.ietf.org/html/rfc6266#appendix-C.1)中也有一个说明。 – bzlm

+0

所以我想知道,为什么它与HttpResponse一起工作?提琴手显示限制已被打破。 –

回答

1

没有便携式和跨浏览器的方式来做到这一点。

请参阅http://greenbytes.de/tech/tc2231/的表格,了解适用于服务器/浏览器/标头的组合。

最好的办法是只Content-Disposition: attachment发送和让浏览器拿起从/path/file.ext URI部分的“默认”的文件名。

+0

谢谢!它现在有效! ))) –