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

前端 未结 6 856
温柔的废话
温柔的废话 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

    Note that in 2011, RFC 6266 (especially Appendix D) weighed in on this issue and has specific recommendations to follow.

    Namely, you can issue a filename with only ASCII characters, followed by filename* with a RFC 5987-formatted filename for those agents that understand it.

    Typically this will look like filename="my-resume.pdf"; filename*=UTF-8''My%20R%C3%A9sum%C3%A9.pdf, where the Unicode filename ("My Résumé.pdf") is encoded into UTF-8 and then percent-encoded (note, do NOT use + for spaces).

    Please do actually read RFC 6266 and RFC 5987 (or use a robust and tested library that abstracts this for you), as my summary here is lacking in important detail.

提交回复
热议问题