Django如何http接收+返回docx文件,文件名中文

 

from django.utils.encoding import escape_uri_path

from django.http import HttpResponse

 

view中函数:

filepath = "文件路径"
with open(filepath, 'rb') as f:
content = f.read()
response = HttpResponse(content)
response['Content-Type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
response['Content-Disposition'] = "attachment;filename*=utf-8''{}".format(escape_uri_path(filepath.split("\\")[-1]))
return response

posted @ 2023-09-18 18:08  无语无奈额  阅读(104)  评论(0编辑  收藏  举报