关键在于转码
from django.http import HttpResponse, StreamingHttpResponse
def download(request):
filename = BASE_DIR + '/statics/upload/模板.xlsx' # 要下载的文件路径
response = StreamingHttpResponse(readFile(filename.encode().decode('unicode_escape').encode('raw_unicode_escape').decode()))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename=模板.xlsx'.encode('utf-8', 'ISO-8859-1')
return response
def readFile(filename,chunk_size=512):
with open(filename,'rb') as f:
while True:
c=f.read(chunk_size)
if c:
yield c
else:
break
|