简体   繁体   English

内容处置文件名中的特殊字符

[英]Special Characters in Content-Disposition filename

My question is a duplicate of How to encode the filename parameter of Content-Disposition header in HTTP?我的问题是如何在 HTTP 中对 Content-Disposition 标头的文件名参数进行编码? But since that 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我开发了一个 C++ CGI 应用程序,它提供的文件可以在名称中包含特殊字符,例如
" weird # € = { } ; filename.txt " "奇怪 # € = { } ; 文件名.txt "

There seems to be no possibility to set the HTTP Content-Dispostion in a way that it works for every browser like似乎不可能以适用于每个浏览器的方式设置 HTTP Content-Dispostion,例如

  • Internet Explorer IE浏览器
  • 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 ; ) Internet Explorer (添加双引号并替换 # 和 ; )

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

Firefox (double quotes seem to work. nothing more to do): Firefox (双引号似乎有效。没什么可做的):

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:使用双引号或使用语法:filename*=UTF-8''... 会产生以下问题:

  • Multiple sticked together spaces in filenames are reduced to one文件名中的多个粘在一起的空格减少为一个
  • { and } disapear: " ab{}cd.txt " -> " abcd.txt " { 和 }消失:“ ab{}cd.txt ”->“ abcd.txt
  • filenames get cut off after ;之后文件名被截断; in it: " abc ; def.txt " -> " abc "其中:“ abc ; def.txt ” -> “ abc

EDIT 2: This was because of filename length limitations.编辑 2:这是因为文件名长度限制。 This syntax works with Opera:此语法适用于 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.转义字符不会被翻译回来,或者浏览器想要使用我的 cgi 应用程序的名称保存到文件中。 That was because my encoding was wrong.那是因为我的编码是错误的。 I did not encode according to RFC 5987. But Safari isn't using this encoding anyway.我没有根据 RFC 5987 进行编码。但是 Safari 无论如何都没有使用这种编码。 So no solution for the € character so far.所以到目前为止还没有针对 € 字符的解决方案。

BTW: An UTF-8 converter http://www.rishida.net/tools/conversion/顺便说一句:UTF-8 转换器http://www.rishida.net/tools/conversion/

I used the latest version of every browser fo these tests:我在这些测试中使用了每个浏览器的最新版本:

  • Firefox 7火狐 7
  • Internet Explorer 9浏览器 9
  • Chrome 15铬 15
  • Opera 11.5歌剧 11.5
  • Safari 5.1 Safari 5.1

PS: I tried all special characters on my keyboard. PS:我在键盘上尝试了所有特殊字符。 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). ü ä ö ß 9.jpg(删除了前 6 个字符)。 EDIT 2: This was because of filename length limitations of the browser.编辑 2:这是因为浏览器的文件名长度限制。 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.我没有深入研究这个问题,但看起来正常的文件名可以长约 200 个字符,而带有许多转义序列的文件名甚至更多但少于 250 个。但这没关系。
  • Opera: 0 !歌剧:0! § $ % & ( ) = ` ´ [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . §$%&()=`´[]²³@€µ^°~+'#-_。 , ; , ; ü ä ö ß 9.jpg (missing some characters as before). ü ä ö ß 9.jpg(像以前一样缺少一些字符)。 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.编辑 2:我缩短了我的测试字符串,因为我怀疑 Opera 的文件名长度“问题”,就像 IE 一样,它也在那里工作。
  • Safari doesn't work with that syntax. Safari 不适用于该语法。 That was excepted.那是例外。

EDIT 2:编辑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!到目前为止的状态是,语法filename*=UTF-8''filname escape sequence"适用于除 Safari 之外的所有浏览器。唯一被 Safari 替换的字符是 €。我想我可以接受。谢谢你!

EDIT 3: Filename length编辑 3:文件名长度

I noticed some filename length issues.我注意到一些文件名长度问题。

  • Internet Explorer: File names can be 147 characters long. Internet Explorer:文件名的长度可以为 147 个字符。 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.生成的文件名少于 147 个字符。 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.我使用了 2 个转义序列,文件名缩短了 5 个字符,我使用了许多转义序列,文件名缩短了 2 个字符。 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.例如,我尝试了 250 个字符,浏览器说我必须减少文件名(Chrome),或者他们自己将其缩短为 220(Opera)或 210(Firefox)字符。 Opera cut off the file ending though. Opera 切断了文件结尾。 Safari tried to save that long file name and ended up not saving it and writing "-1" in the download list as filename. Safari 试图保存那个长文件名,但最终没有保存它并在下载列表中将“-1”写为文件名。

Firefox, MSIE (starting with version 9), Opera, Konq and Chrome support; Firefox、MSIE(从版本 9 开始)、Opera、Konq 和 Chrome 支持; MSIE8 and Safari not support;不支持 MSIE8 和 Safari; others support is unknown - the encoding defined in RFC 5987.其他支持未知 - 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).它的 unicode 代码点不是 %80,修复这个应该使它在除 Safari 之外的任何地方都能工作(正确的编码是 %e2%82%ac)。

Test case at:测试用例在:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM