Special Characters in Content-Disposition filename(Content-Disposition 文件名中的特殊字符)
问题描述
我的问题是 How to encode the filename parameter of Content-Disposition header in HTTP?但是由于这个问题是很久以前提出的并且仍然没有令人满意的答案(在我看来),我想再问一次.
My question is a duplicate of How to encode the filename parameter of Content-Disposition header in HTTP? 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.
我开发了一个 C++ CGI 应用程序,它提供的文件名称中可能包含特殊字符,例如
"weird # € = { } ; filename.txt"
I develop a C++ CGI application that delivers files that can contain special characters in their names like
"weird # € = { } ; filename.txt"
似乎不可能以适用于每个浏览器的方式设置 HTTP Content-Dispostion,例如
There seems to be no possibility to set the HTTP Content-Dispostion in a way that it works for every browser like
- Internet Explorer
- 火狐
- 铬
- 歌剧
- 野生动物园
我很乐意为每个浏览器提供不同的解决方案.
现在这就是我走了多远:
I would be happy with a different solution for every browser.
Now that is how far I came:
Internet Explorer(添加双引号并替换 # 和 ; )
Internet Explorer (added double quotes and replaced # and ; )
Content-Disposition: attachment; filename="weird %23 € = { } %3B filename.txt"
Firefox(双引号似乎可以工作.没什么可做的):
Firefox (double quotes seem to work. nothing more to do):
Content-Disposition: attachment; filename="weird # € = { } ; filename.txt"
另一种可行的选择:
Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt
铬
当只使用双引号时会出现这些问题:
when using only double quotes these problems arise:
- = 在文件名中消失
- € 将被替换为 -
但这有效:
Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%e2%82%ac%20%3D%20%7B%20%7D%20%3B%20filename.txt
歌剧
使用双引号或使用语法:filename*=UTF-8''... 会产生以下问题:
Using duoble quotes or using the syntax: filename*=UTF-8''... produces the following problems:
- 文件名中多个粘贴在一起的空格减少为一个
- { 和 } 消失:ab{}cd.txt" -> abcd.txt"
- 文件名在 ; 之后被截断其中:abc ; def.txt"->abc"
- 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"
编辑 2: 这是因为文件名长度限制.此语法适用于 Opera:
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
对我不起作用.转义字符不会被翻译回来,或者浏览器想要以我的 cgi 应用程序的名称保存到文件中.那是因为我的编码错误.我没有根据 RFC 5987 进行编码.但是 Safari 无论如何都没有使用这种编码.所以到目前为止还没有解决 € 字符的方法.
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.
顺便说一句:UTF-8 转换器 http://www.rishida.net/tools/conversion/
BTW: An UTF-8 converter http://www.rishida.net/tools/conversion/
我在这些测试中使用了每个浏览器的最新版本:
I used the latest version of every browser fo these tests:
- 火狐7
- Internet Explorer 9
- Chrome 15
- Opera 11.5
- Safari 5.1
PS:我尝试了键盘上的所有特殊字符.我在这个线程中只使用了那些制造麻烦的.
PS: I tried all special characters on my keyboard. 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:
完整的测试字符串:
0 ! § $ % & ( ) = ` ´ { } [ ] ² ³ @ € µ ^ ° ~ + ' # - _ . , ; ü ä ö ß 9.jpg
编码测试字符串:
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
使用这种方法:
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
我得到了以下结果:
- Firefox 有效
- Chrome 工作
- IE: $ % &( ) = ` ´ { } [ ] ² ³ @ € µ ^ ° ~ + ' # - _ ., ;ü ä ö ß 9.jpg(删除前 6 个字符).编辑 2: 这是因为浏览器的文件名长度限制.它开始从字符串的开头切断文件名.我没有深入研究,但看起来普通文件名可能有大约 200 个字符长,而具有许多转义序列的文件名甚至更多但少于 250 个.但没关系.
- 歌剧:0 !§ $ % &( ) = ` ´ [ ] ² ³ @ € µ ^ ° ~ + ' # - _ ., ;ü ä ö ß 9.jpg(像以前一样缺少一些字符).编辑 2: 我缩短了我的测试字符串,因为我怀疑 Opera 的文件名长度问题"与 IE 一样,它也在那里工作.
- Safari 不适用于该语法.那是例外.
- 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.
目前的状态是,语法 filename*=UTF-8''filname escape sequence" 适用于除 Safari 之外的所有浏览器.唯一被 Safari 替换的字符是 €. 我想我可以忍受.谢谢!
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!
我注意到一些文件名长度问题.
I noticed some filename length issues.
- Internet Explorer:文件名长度可以为 147 个字符.如果字符串不包含转义序列,那么这就是文件名的长度.如果是这样,文件名可能会有所不同.生成的文件名少于 147 个字符.但它不同.我使用了 2 个转义序列,文件名缩短了 5 个字符,我使用了许多转义序列,文件名缩短了 2 个字符.我在这里找不到规则.
- 其他浏览器似乎没有这个问题.如果文件系统可以处理文件,他们会保存文件.例如,我尝试了 250 个字符,浏览器说我必须减少文件名(Chrome),或者他们自己将其缩短为 220(Opera)或 210(Firefox)字符.Opera 切断了文件的结尾.Safari 尝试保存那个长文件名,但最终没有保存它,而是在下载列表中写入-1"作为文件名.
推荐答案
Firefox、MSIE(从版本 9 开始)、Opera、Konq 和 Chrome 支持;MSIE8 和 Safari 不支持;其他支持未知 - RFC 5987 中定义的编码.
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.
注意在
Content-Disposition: attachment; filename*=UTF-8''weird%20%23%20%80%20%3D%20%7B%20%7D%20%3B%20filename.txt
你把欧元字符的编码弄错了;它的 unicode 代码点不是 %80,修复这个问题应该可以使它在除 Safari 之外的任何地方都能工作(正确的编码是 %e2%82%ac).
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).
测试用例在:
http://greenbytes.de/tech/tc2231/#attwithfn2231utf8
这篇关于Content-Disposition 文件名中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Content-Disposition 文件名中的特殊字符
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01