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

发布时间 2023-09-18 18:16:16作者: 无语无奈额

 

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