遇到名称不显示,乱码,无后缀等问题,多半是编码和解码不对产生的。
?
def read_file(file_name, size):
with open(file_name, mode='rb') as fp:
while True:
c = fp.read(size)
if c:
yield c
else:
break
from django.shortcuts import get_object_or_404
from django.http import StreamingHttpResponse
import os
def getDoc(request, id):
doc = get_object_or_404(Doc, id=id)
update_to, filename = str(doc.file).split('/')
filepath = '%s/media/%s/%s' % (os.getcwd(), update_to, filename)
response = StreamingHttpResponse(read_file(filepath, 512))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{}"'.format(filename.encode('utf-8').decode('ISO-8859-1'))
return response
.format(filename.encode('utf-8').decode('ISO-8859-1'))
亏我找了那么久,原来就差这几个字。
|