Content-Disposition标头中的Unicode

我正在使用在HttpHandler子项中实现的HttpContext对象来下载文件,当我在文件名中有非ascii字符时,它在IE中看起来很奇怪,而在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一直没用。

即它是’ß””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””’ Ô””””””””””””””””””””’

任何想法如何解决这个问题?

我有类似的问题。 您必须使用HttpUtility.UrlEncode或Server.UrlEncode来编码文件名。 另外我记得firefox不需要它。 此外,当它是url编码时,它会破坏文件名。 我的代码:

 // 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声明:

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

但后来我发现[RFC 2045]已过时的参考资料,必须参考RFC 2231 ,其中规定:

重复使用星号(“*”)来提供语言和字符集信息存在以及正在使用编码的指示符。 单引号(“’”)用于在参数值的开头界定字符集和语言信息。 百分号(“%”)用作编码标志,与RFC 2047一致。

这意味着您可以将UrlEncode用于非ascii符号,只要您包含rfc中所述的编码即可。 这是一个例子:

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

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

HttpUtility.UrlPathEncode可能是更好的选择。 由于URLEncode将用’+’符号替换空格。

对我来说,这个解决方案适用于所有主流浏览器:

 Response.AppendHeader("Content-Disposition", string.Format("attachment; filename*=UTF-8''{0}", HttpUtility.UrlPathEncode(fileName).Replace(",", "%2C")); var mime = MimeMapping.GetMimeMapping(fileName); return File(fileName, mime); 

使用ASP.NET MVC 3。

替换是必要的,因为Chrome在参数值中不喜欢逗号(,): http : //www.gangarasa.com/lets-Do-GoodCode/tag/err_response_headers_multiple_content_disposition/

您可能需要阅读RFC 6266并查看http://greenbytes.de/tech/tc2231/上的测试。

对我来说这解决了这个问题:

 var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(data) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileNameStar = "foo-ä-€.html" }; 

当我看到fiddler中的repsonse时,我可以看到文件名已经自动使用UTF-8进行编码:

使用UTF-8编码的Content-Disposition文件名的Fiddler响应示例

如果我们查看Content-Disposition标题的值,我们可以看到它与@Johannes Geyer的答案相同。 唯一的区别是我们没有自己编码,ContentDispositionHeaderValue类负责处理。

我使用了Testcases for Content-Disposition标题: http : //greenbytes.de/tech/tc2231/,如Julian Reschke所述。 有关ContentDispositionHeaderValue类的信息可以在MSDN上找到。

对于Asp.Net Core(本文第2版)UrlPathEncode已弃用,以下是如何实现所需结果:

 System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition { FileName = Uri.EscapeUriString(fileName), Inline = true // false = prompt the user for downloading; true = browser to try to show the file inline }; Response.Headers.Add("Content-Disposition", cd.ToString());