How to encode UTF8 filename for HTTP headers? (Python, Django)(如何为 HTTP 标头编码 UTF8 文件名?(Python,Django))
问题描述
我对 HTTP 标头有疑问,它们是用 ASCII 编码的,我想提供一个视图来下载名称可以是非 ASCII 的文件.
response['Content-Disposition'] = '附件;文件名="%s"' % (vo.filename.encode("ASCII","replace"), )我不想使用静态文件来解决非 ASCII 文件名的相同问题,但在这种情况下,文件系统和文件名编码会出现问题.(我不知道目标操作系统.)
我已经尝试过 urllib.quote(),但是它引发了 KeyError 异常.
可能我做错了什么,但也许这是不可能的.
这是一个常见问题.
没有可互操作的方法来做到这一点.一些浏览器实现专有扩展(IE、Chrome),另一些实现 RFC 2231(Firefox、Opera).
请参阅 http://greenbytes.de/tech/tc2231/ 上的测试用例..p>
更新:截至 2012 年 11 月,所有当前桌面浏览器都支持 RFC 6266 和 RFC 5987 中定义的编码(Safari >= 6、IE >= 9、Chrome、Firefox、Opera、Konqueror).
I have problem with HTTP headers, they're encoded in ASCII and I want to provided a view for downloading files that names can be non ASCII.
response['Content-Disposition'] = 'attachment; filename="%s"' % (vo.filename.encode("ASCII","replace"), )
I don't want to use static files serving for same issue with non ASCII file names but in this case there would be a problem with File system and it's file name encoding. (I don't know target os.)
I've already tried urllib.quote(), but it raises KeyError exception.
Possibly I'm doing something wrong but maybe it's impossible.
This is a FAQ.
There is no interoperable way to do this. Some browsers implement proprietary extensions (IE, Chrome), other implement RFC 2231 (Firefox, Opera).
See test cases at http://greenbytes.de/tech/tc2231/.
Update: as of November 2012, all current desktop browsers support the encoding defined in RFC 6266 and RFC 5987 (Safari >= 6, IE >= 9, Chrome, Firefox, Opera, Konqueror).
这篇关于如何为 HTTP 标头编码 UTF8 文件名?(Python,Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 HTTP 标头编码 UTF8 文件名?(Python,Django)
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
