要触发下载,你需要设置
Content-Disposition标题:
from django.http import HttpResponsefrom wsgiref.util import FileWrapper# generate the fileresponse = HttpResponse(FileWrapper(myfile.getvalue()), content_type='application/zip')response['Content-Disposition'] = 'attachment; filename=myfile.zip'return response
如果你不想将文件放在磁盘上,则需要使用
StringIO
import cStringIO as StringIOmyfile = StringIO.StringIO()while not_finished: # generate chunk myfile.write(chunk)
你也可以选择设置
Content-Length标头:
response['Content-Length'] = myfile.tell()



