[英]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,例如
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:仅使用双引号时会出现这些问题:
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''... 会产生以下问题:
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:我在这些测试中使用了每个浏览器的最新版本:
PS: I tried all special characters on my keyboard. PS:我在键盘上尝试了所有特殊字符。 I used in this thread only the ones that made trouble.
我在这个线程中只使用了那些制造麻烦的。
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:我有以下结果:
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 替换的字符是 €。我想我可以接受。谢谢你!
I noticed some filename length issues.我注意到一些文件名长度问题。
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.