看看这个:
import zipfile
from cStringIO import StringIO
from contextlib import closing
from django.http import HttpResponse
def foo(request):
...
with closing(StringIO()) as ff:
zipped = zipfile.ZipFile(ff, 'a')
for i, wb in enumerate(workbooks):
with closing(StringIO()) as f:
wb.save(f)
zipped.writestr('%s.xls' % (i + 1), f.getvalue())
zipped.close()
foo = ff.getvalue()
filename = 'foo.zip'
response = HttpResponse(mimetype="application/zip")
response['Content-Disposition'] = "attachment;filename=%s" % filename
response.write(foo)
return response