我想使用 Django 的
httpresponse
方法下载文件。文件名中包含一些特殊字符,例如中文。我可以使用以下代码下载文件,但是文件名显示为“%E6%B8%B8%E6%88%8F%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B3%A8%E5%86%8C%E9%A1%B5%E9%9D%A2.jpg”。
请问有人知道如何转换文件名吗?response = HttpResponse(attachment.file, content_type='text/plain',mimetype='application/octet-stream')
response['Content-Disposition'] = "attachment; filename="+urlquote(filename)
return response
编辑:
使用 smart_str
时,另一个问题出现了:文件名可以在 Firefox 和 Chrome 中正常显示,但在 IE 中却仍然显示一些未知字符。有没有人知道如何解决这个问题?
提前感谢!
---通过在 IE 和其他浏览器中分别使用 urlquote
和 smart_str
来解决。