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