Login

Tag "sendfile"

Snippet List

Django nginx sendfile example

Use nginx sendfile (X-Accel-Redirect) function to serve files but pass traffic through django. Can be used to serve media files only to logged-in users.

  • django
  • media
  • sendfile
  • nginx
Read More

HttpResponseSendfile

An HttpResponse for giving the user a file download, taking advantage of X-Sendfile if it's available, using FileWrapper if not. Usage: HttpResponseSendfile('/path/to/file.ext') To bypass the fallback: HttpResponseSendfile('/path/to/file.ext', fallback=False) This has been tested working with Lighttpd 1.4.28's mod_fastcgi.

  • download
  • sendfile
  • x-sendfile
Read More

Send large files through Django, and how to generate Zip files

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: http://www.djangoproject.com/documentation/modpython/#serving-media-files

  • sendfile
  • zipfile
  • tempfile
  • temporaryfile
  • filewrapper
Read More

3 snippets posted so far.