- August 12, 2007
- sendfile zipfile tempfile temporaryfile filewrapper
- 11 (after 11 ratings)
This snippet demonstrates how you can send a file (or file-like object) through Django without having to load the whole thing into memory. The FileWrapper will turn the file-like object into an iterator for chunks of 8KB.
This is a full working example. Start a new app, save this snippet as views.py, and add the views to your URLconf. The send_file view will serve the source code of this file as a plaintext document, and the send_zipfile view will generate a Zip file with 10 copies of it.
Use this solution for dynamic content only, or if you need password protection with Django's user accounts. Remember that you should serve static files directly through your web server, not through Django:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import os, tempfile, zipfile from django.http import HttpResponse from django.core.servers.basehttp import FileWrapper def send_file(request): """ Send a file through Django without loading the whole file into memory at once. The FileWrapper will turn the file object into an iterator for chunks of 8KB. """ filename = __file__ # Select your file here. wrapper = FileWrapper(file(filename)) response = HttpResponse(wrapper, content_type='text/plain') response['Content-Length'] = os.path.getsize(filename) return response def send_zipfile(request): """ Create a ZIP file on disk and transmit it in chunks of 8KB, without loading the whole file into memory. A similar approach can be used for large dynamic PDF files. """ temp = tempfile.TemporaryFile() archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED) for index in range(10): filename = __file__ # Select your files here. archive.write(filename, 'file%d.txt' % index) archive.close() wrapper = FileWrapper(temp) response = HttpResponse(wrapper, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename=test.zip' response['Content-Length'] = temp.tell() temp.seek(0) return response
More like this
- KMZMiddleware by giovabal 7 years, 7 months ago
- Create OpenOffice documents by king 8 years, 9 months ago
- Generate Model Data. Lots of Options. by bl4th3rsk1t3 5 years, 8 months ago
- Export queryset to Excel workbook by masida 1 year, 2 months ago
- Serve static media and indexes from app directories [Python2.5, Development only] by adamlofts 7 years, 3 months ago