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