- August 12, 2007
- 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
- LazyPrimaryKeyRelatedField by LLyaudet 1 week ago
- CacheInDictManager by LLyaudet 1 week ago
- MYSQL Full Text Expression by Bidaya0 1 week, 1 day ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks ago
- Django Standard API Response Middleware for DRF for modern frontend easy usage by Denactive 1 month ago