标签:需要 odi 文件 nts after hose seq opera file
今天在迁移一个老系统到新服务器的时候,在测试下载接口的时候发现:同一个接口,有些文件可以下载,有些文件不能下载。
调试了半天发现,好像是文件名的问题。
string fileName = "38374-fjf(1).13jk"; string contentDisposition = string.Format("attachment;filename={0}",fileName); msg.Content.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse(contentDisposition);
于是乎悲剧了。
然后我百度了一下,网上似乎都是什么书说文件名乱码的,没有一个说说是我这种清空情况。
无奈去查了一下标准文档 http://www.ietf.org/rfc/rfc6266.txt
跳过前面的定义直接看例子
5. Examples Direct the UA to show "save as" dialog, with a filename of "example.html": Content-Disposition: Attachment; filename=example.html Direct the UA to behave as if the Content-Disposition header field wasn‘t present, but to remember the filename "an example.html" for a subsequent save operation: Content-Disposition: INLINE; FILENAME= "an example.html" Note: This uses the quoted-string form so that the space character can be included. Direct the UA to show "save as" dialog, with a filename containing the Unicode character U+20AC (EURO SIGN): Content-Disposition: attachment; filename*= UTF-8‘‘%e2%82%ac%20rates Here, the encoding defined in [RFC5987] is also used to encode the non-ISO-8859-1 character. This example is the same as the one above, but adding the "filename" parameter for compatibility with user agents not implementing RFC 5987: Content-Disposition: attachment; filename="EURO rates"; filename*=utf-8‘‘%e2%82%ac%20rates Note: Those user agents that do not support the RFC 5987 encoding ignore "filename*" when it occurs after "filename".
Note: This uses the quoted-string form so that the space character
can be included.
看到这句我似乎发现了什么。如果有空格就需要写成字符串引用的格式。
string contentDisposition = string.Format("attachment;filename=\"{0}\"", fileName);
文件名有特殊符号是解决了,比如括号,空格什么的。但是如果文件名当中有中午,下载时候的默认文件名缺还是乱码。
这个网上有一堆解决方案。
string fileName= HttpUtility.UrlEncode("啊fnam(4)e.ext",UTF8Encoding.UTF8);
其实只要对文件名做一次Encode转码就行了。
就这个简单的问题,耗费了我一个小时。
今天就先到这里,晚安。
一个文件下载接口引发的血案Content-Disposition之殇
标签:需要 odi 文件 nts after hose seq opera file
原文地址:http://www.cnblogs.com/momoxingchen/p/6493317.html