django - 使用HttpResponse在Django中下载名称中包含中文字符的文件

标签 django file download cjk

我想用 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中仍然显示一些未知字符。有谁知道如何解决这个问题?

提前致谢!

---通过在IE和其他浏览器中使用urlquotesmart_str解决

最佳答案

我觉得可能和Encoding Translated Strings有关

试试这个:

    from django.utils.encoding import smart_str, smart_unicode
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(filename)
    return response

关于django - 使用HttpResponse在Django中下载名称中包含中文字符的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9531067/

相关文章:

django - django+gunicorn+nginx 配置上的错误网关

django - 打包 django 应用程序的好方法是什么?

java - 写入文件时出现 IndexOutOfBoundException

php 下载不适用于 &

python - Django:将文件添加到表单会出现多个参数错误

javascript - 防止浏览器开发工具更改 POST 值

Linux 如何删除组为 Contoso 的所有文件和文件夹

file - 如何将 bash 参数传递给 awk 脚本?

ios - 如何在 iOS 中通过经过身份验证的 url 下载文件?

python - imap_tools - 在 python 中下载附件