Python3.x在django中如何设置Content-Disposition,才能让浏览器正确保存中文命名的文件?


在网上找到了解决方案,使用下面的代码,文件名成功显示了中文。

from django.utils.encoding import escape_uri_path
from django.http import HttpResponse
def test(request):
file_name = '测试.txt'
content = ...
response = HttpResponse(content, content_type='application/octet-stream')
response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(file_name))
return response


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM