如何编码HTTP标头的UTF8文件名? (Python,Django) [英] How to encode UTF8 filename for HTTP headers? (Python, Django)

查看:140
本文介绍了如何编码HTTP标头的UTF8文件名? (Python,Django)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HTTP头的问题,它们是用ASCII编码的,我想提供一个用于下载文件的视图,名称可以是非ASCII。

  response ['Content-Disposition'] ='attachment; filename =%s'%(vo.filename.encode(ASCII,replace),)

我不想使用与非ASCII文件名相同问题的静态文件,但在这种情况下,文件系统和文件名编码会出现问题。 (我不知道目标os。)



我已经尝试过urllib.quote(),但它引发了KeyError异常。



可能我做错了,但也许是不可能的。

解决方案

这是一个常见问题。 / p>

没有可互操作的方式来做到这一点。一些浏览器实现专有扩展(IE,Chrome),其他实现RFC 2231(Firefox,Opera)。



请参阅 http://greenbytes.de/tech/tc2231/



更新:as of 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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