Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the "best of both worlds" you could combine S.Lott's solution with the <a href="http://www.google.com.ng/search?q=mod_xsendfile&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" rel="noreferrer">xsendfile module</a>: django generates the path to the file (or the file itself), but the actual file serving is handled by Apache/Lighttpd. Once you've set up mod_xsendfile, integrating with your view takes a few lines of code:</p> <pre><code>from django.utils.encoding import smart_str response = HttpResponse(mimetype='application/force-download') # mimetype is replaced by content_type for django 1.7 response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) response['X-Sendfile'] = smart_str(path_to_file) # It's usually a good idea to set the 'Content-Length' header too. # You can also set any other required headers: Cache-Control, etc. return response </code></pre> <p>Of course, this will only work if you have control over your server, or your hosting company has mod_xsendfile already set up.</p> <p><strong>EDIT:</strong></p> <blockquote> <p>mimetype is replaced by content_type for django 1.7</p> </blockquote> <pre><code>response = HttpResponse(content_type='application/force-download' </code></pre> <p><strong>EDIT:</strong> For <code>nginx</code> check <a href="https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/" rel="noreferrer">this</a>, it uses <code>X-Accel-Redirect</code> instead of <code>apache</code> X-Sendfile header.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload