How to encode UTF8 filename for HTTP headers? (Python, Django)

前端 未结 6 835
温柔的废话
温柔的废话 2020-11-28 04:18

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[\'Cont         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 04:56

    Don't send a filename in Content-Disposition. There is no way to make non-ASCII header parameters work cross-browser(*).

    Instead, send just “Content-Disposition: attachment”, and leave the filename as a URL-encoded UTF-8 string in the trailing (PATH_INFO) part of your URL, for the browser to pick up and use by default. UTF-8 URLs are handled much more reliably by browsers than anything to do with Content-Disposition.

    (*: actually, there's not even a current standard that says how it should be done as the relationships between RFCs 2616, 2231 and 2047 are pretty dysfunctional, something that Julian is trying to get cleared up at a spec level. Consistent browser support is in the distant future.)

提交回复
热议问题