Version 2 (modified by 18 years ago) ( diff ) | ,
---|
Return a dynamically created zip file in your HttpResponse.
import zipfile from cStringIO import StringIO from django.utils.httpwrappers import HttpResponse def view_that_returns_zipped_file(request): response = HttpResponse(mimetype='application/zip') response['Content-Disposition'] = 'filename=all_things.zip' #first assemble your files files = [] for thing in Thing.objects.all(): files.append(("%s.pdf" % (thing.id,), thing.biggish_file())) #now add them to a zip file #note the zip only exist in memory as you add to it buffer = StringIO() zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) for name, f in files: zip.writestr(name, f) zip.close() buffer.flush() #the import detail--we return the content of the buffer ret_zip = buffer.getvalue() buffer.close() response.write(ret_zip) return response
Note:
See TracWiki
for help on using the wiki.