在内容配置文件名中的特殊字符 [英] Special Characters in Content-Disposition filename

查看:468
本文介绍了在内容配置文件名中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这线程是如何连接code含量-Disposition头的HTTP?中的文件名参数
但由于这个问题是很久以前的要求,仍然没有令人满意的答案(在我看来),我想再问。

我开发C ++ CGI应用程序,提供了可以包含他们的名字,如搜索特殊字符的文件怪异#€= {}; FILENAME.TXT

有似乎没有可能设置HTTP内容Dispostion的方式,它适用于所有的浏览器,如


  • 的Internet Explorer

  • 火狐


  • 歌剧

  • Safari浏览器

我会很乐意与每个浏览器不同的解决方案。结果
现在,这是我多远就来了:

的Internet Explorer (加双引号,取而代之的#和;)

 内容处置:附件;文件名=怪异%23€= {}%3B FILENAME.TXT

火狐(双引号似乎工作无事可做。):

 内容处置:附件;文件名=#怪异€= {}; FILENAME.TXT

另一种替代性的工作:

 内容处置:附件;文件名* = UTF-8''weird%20%23%20%E2%82%AC%20%3D%20%7B%20%7D%20%3B%20filename.txt

Chrome浏览器

只使用双引号时,这些问题就出来了:


    在文件名中
  • =在前看不见

  • €将被替换 - 结果

但这个作品:

 内容处置:附件;文件名* = UTF-8''weird%20%23%20%E2%82%AC%20%3D%20%7B%20%7D%20%3B%20filename.txt

歌剧

使用duoble引号或使用的语法:文件名* = UTF-8''...产生以下问题:


  • 在文件名中的多粘在一起空间减少到一个

  • {和} disapear: AB {} cd.txt - > abcd.txt

  • 文件名后得到切断;在它: ABC; def.txt - > ABC

编辑2:这是因为文件名长度的限制。这个语法适用于歌剧:

 内容处置:附件;文件名* = UTF-8''weird%20%23%20%E2%82%AC%20%3D%20%7B%20%7D%20%3B%20filename.txt

Safari浏览器


  • €将​​由invisble字符替换(使用双引号)

     无解的prevents的小问题


从其他线程的建议使用(上述)

 内容处置:附件;文件名* = UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt

我没有工作。转义字符不会被翻译回或浏览器要救我的CGI应用程序的名称为文件。那是因为我的编码是错误的。我根据RFC 5987.但是Safari浏览器没有使用这种编码反正也没带code。所以对于€字符无解为止。

BTW:一个UTF-8转换 http://www.rishida.net/tool​​s/conversion/

我用的是最新版本的每个浏览器的FO这些测试:


  • 火狐7

  • Internet Explorer 9的

  • 铬15

  • 歌剧院11.5

  • Safari浏览器5.1

PS:我想我的键盘上的所有特殊字符。我在此线程只是做麻烦的人使用。

编辑:

我也试图与我的键盘上所有特殊字符(在文件名都可以使用)的文件名,并因为它与上面的测试字符串这样做,没有工作:

完整的测试字符串:

  0! §$%&安培; ()=''{} []²³@€μ^°〜+'# -  _。 ; ü邻ß9.jpg

恩codeD测试字符串:

<$p$p><$c$c>0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg

使用这个方法:

 内容处置:附件; filename*=UTF-8''0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg

我有以下结果:


  • 火狐作品

  • Chrome的工作

  • IE:$%&安培; ()=''{} []²³@€μ^°〜+'# - _。 ; ü邻ß9.jpg(去除前6个字符)。 编辑2:这是因为浏览器的文件名长度的限制。它startet从字符串的开头切断的文件名。我没有深入到这一点,但它看起来像正常的文件名可以是约200个字符长文件名,并与许多逃生sequesnces甚至以上,但少于250不过没关系。

  • 歌剧:0! §$%&安培; ()=''[]²³@€μ^°〜+'# - _。 ; ü邻ß9.jpg(如前缺少一些字符)。 编辑2:我缩短了我的测试字符串,因为我怀疑文件名长度问题与歌剧因为有与IE浏览器,它也有工作

  • Safari不与语法的工作。那是例外。

编辑2:

状态到目前为止,该语法文件名* = UTF-8''filname转义序列,除了每天的Safari浏览器,而这是越来越Safari浏览器取代了唯一字符的工作原理是€我想我可以忍受的。谢谢!

编辑3:文件名长度

我注意到一些文件名长度的问题。


  • Internet Explorer中:文件名可以是147个字符。如果字符串不包含转义序列那么这就​​是文件名的长度。如果是这样的文件名可能会有所不同。由此产生的文件名是短于147个字符。但有所不同。我用了2转义序列和文件名缩短为5个字符,我使用了许多转义序列,缩短磺酰2个字符的文件名。我不能在这里找到的规则。

  • 其他浏览器不似乎有问题。他们将保存文件,如果文件系统可以处理它。我试过,例如250个字符,并在浏览器说我来减少文件名(浏览器),或者他们这样做是themselfs它缩短要么220(歌剧)或210(火狐)字符。歌剧切断虽​​然结尾的文件。 Safari浏览器试图保存长文件名,结束了不保存,并在下载列表中的文件名写为-1。


解决方案

火狐,MSIE(从9版),歌剧院,Konq和Chrome支持; MSIE8和Safari不支持;他人的支持是未知的 - 在RFC 5987中定义的编码

请注意,在

 内容处置:附件;文件名* = UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt

您得到了欧元字符错误编码;它是单向code code点不是80%,解决这个应该让工作无处不在,除了Safari浏览器(正确的编码为%E2%82%AC)。

在测试用例:

<一个href=\"http://greenbytes.de/tech/tc2231/#attwithfn2231utf8\">http://greenbytes.de/tech/tc2231/#attwithfn2231utf8

This thread is a duplicate of How to encode the filename parameter of Content-Disposition header in HTTP? But since this question was asked a long time ago and there is still no satisfying answer (in my opinion), I would like to ask again.

I develop a C++ CGI application that delivers files that can contain special characters in their names like
"weird # € = { } ; filename.txt"

There seems to be no possibility to set the HTTP Content-Dispostion in a way that it works for every browser like

  • Internet Explorer
  • Firefox
  • Chrome
  • Opera
  • Safari

I would be happy with a different solution for every browser.
Now that is how far I came:

Internet Explorer (added double quotes and replaced # and ; )

Content-Disposition: attachment; filename="weird %23 € = { } %3B filename.txt"

Firefox (double quotes seem to work. nothing more to do):

Content-Disposition: attachment; filename="weird # € = { } ; filename.txt"

Another working alternative:

Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt

Chrome

when using only double quotes these problems arise:

  • = disapears in filenames
  • € will be replaced by -

but this works:

Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt

Opera

Using duoble quotes or using the syntax: filename*=UTF-8''... produces the following problems:

  • Multiple sticked together spaces in filenames are reduced to one
  • { and } disapear: "ab{}cd.txt" -> "abcd.txt"
  • filenames get cut off after ; in it: "abc ; def.txt" -> "abc"

EDIT 2: This was because of filename length limitations. This syntax works with Opera:

Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt

Safari

  • € will be replaced by an invisble character (using double quotes)

    no solution that prevents that little problem
    

The suggestion from the other thread (mentioned above) using

Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt

didn't work for me. The escape characters won't be translated back or the browser wants to save to file with the name of my cgi application. That was because my encoding was wrong. I did not encode according to RFC 5987. But Safari isn't using this encoding anyway. So no solution for the € character so far.

BTW: An UTF-8 converter http://www.rishida.net/tools/conversion/

I used the latest version of every browser fo these tests:

  • Firefox 7
  • Internet Explorer 9
  • Chrome 15
  • Opera 11.5
  • Safari 5.1

PS: I tried all special characters on my keyboard. I used in this thread only the ones that made trouble.

EDIT:

I also tried a filename with all special characters on my keyboard (that are possible in a filename) and that did not work as it did with the test string above:

Complete Test string:

0 ! § $ % & ( ) = ` ´ { }    [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg

Encoded Test String:

0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg

Using this method:

Content-Disposition: attachment; filename*=UTF-8''0%20%21%20%C2%A7%20%24%20%25%20%26%20%28%20%29%20%3D%20%60%20%C2%B4%20%7B%20%7D%20%20%20%20%5B%20%5D%20%C2%B2%20%C2%B3%20%40%20%E2%82%AC%20%C2%B5%20%5E%20%C2%B0%20~%20%2B%20%27%20%23%20-%20_%20.%20%2C%20%3B%20%C3%BC%20%C3%A4%20%C3%B6%20%C3%9F%209.jpg

I had the following results:

  • Firefox works
  • Chrome works
  • IE: $ % & ( ) = ` ´ { } [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg (removed the first 6 characters). EDIT 2: This was because of filename length limitations of the browser. It startet to cut off the filename from the start of the string. I didn't go deep into this but it looks like normal filenames can be about 200 characters long and filenames with many escape sequesnces even more but less than 250. But that's OK.
  • Opera: 0 ! § $ % & ( ) = ` ´ [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg (missing some characters as before). EDIT 2: I shortened my test string because I suspected filename length "problems" with Opera as there are with IE and it worked there too.
  • Safari doesn't work with that syntax. That was excepted.

EDIT 2:

Status so far is, that the syntax filename*=UTF-8''filname escape sequence" works with every browser except Safari. And the only character that is getting replaced with Safari is the €. I guess I can live with that. Thank you!

EDIT 3: Filename length

I noticed some filename length issues.

  • Internet Explorer: File names can be 147 characters long. If the string doesn't contain escape sequences then that's the length of the filename. If it does the file name can vary. The resulting file name is shorter that 147 characters. But it differs. I used 2 escape sequences and the file name shortened 5 characters and I used many escape sequences and the file name shortened onyl 2 characters. I couldn't find a rule here.
  • The other browsers don't seems to have that problem. They would save the file if the file system can handle it. I tried for instance 250 characters and the browsers said I have to reduce the file name (Chrome) or they did it themselfs shortening it to either 220 (Opera) or 210 (Firefox) characters. Opera cut off the file ending though. Safari tried to save that long file name and ended up not saving it and writing "-1" in the download list as filename.

解决方案

Firefox, MSIE (starting with version 9), Opera, Konq and Chrome support; MSIE8 and Safari not support; others support is unknown - the encoding defined in RFC 5987.

Note that in

  Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt

you got the encoding for the Euro character wrong; it's unicode code point is not %80, fixing this should make it work everywhere except Safari (the correct encoding being %e2%82%ac).

Test case at:

http://greenbytes.de/tech/tc2231/#attwithfn2231utf8

这篇关于在内容配置文件名中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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