Having Django serve downloadable files
For the "best of both worlds" you could combine S.Lott's solution with the xsendfile module: 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:
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
Of course, this will only work if you have control over your server, or your hosting company has mod_xsendfile already set up.
EDIT:
mimetype is replaced by content_type for django 1.7
response = HttpResponse(content_type='application/force-download')
EDIT:
For nginx
check this, it uses X-Accel-Redirect
instead of apache
X-Sendfile header.
Serve a file for download with Django
You want to set the Content-disposition header to "attachment" (and set the proper content-type too - from the var names I assume those files are android packages, else replace with the proper content type):
response = HttpResponse(fh, content_type="application/vnd.android.package-archive")
response["Content-disposition"] = "attachment; filename={}".format(os.path.basename(path_to_apk))
return response
Django Serving a Download File
Have you considered just sending p.body
through the response
like this:
response = HttpResponse(mimetype='text/plain')
response['Content-Disposition'] = 'attachment; filename="%s.txt"' % p.uuid
response.write(p.body)
Django download a file
You missed underscore in argument document_root. But it's bad idea to use serve
in production. Use something like this instead:
import os
from django.conf import settings
from django.http import HttpResponse, Http404
def download(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
How can I return a downloadable file Django
a
tag has one not so well-known attribute called: download:
<a href="{% url 'my_file_link' %}" download>download the file</a>
^ here
other way is to send header Content-disposition: "attachment"
from server. If you are serving static files via Django (you probably shouldnt), do it in your view where you're composing the response. If you are serving the files from your webserver directly, here's an example for Nginx:
location /media {
access_log off;
root /var/www/project/media;
location /media/downloads/ {
# add the "force download" header for files in this directory
add_header Content-disposition "attachment";
}
}
Related Topics
Output to the Same Line Overwriting Previous Output
Order' of Unordered Python Sets
Import CSV File as a Pandas Dataframe
Can't Set Attributes on Instance of "Object" Class
Scatter Plots in Pandas/Pyplot: How to Plot by Category
What Are the Risks of Running 'Sudo Pip'
Apply Function to Each Element of a List
How to Access Variables from Different Classes in Tkinter
How to Convert a String to a Number If It Has Commas in It as Thousands Separators
How Exactly Does a Generator Comprehension Work
Create Own Colormap Using Matplotlib and Plot Color Scale
How to Merge a List of Dicts into a Single Dict
How to Ignore the First Line of Data When Processing CSV Data
Why Is My Pygame Application Loop Not Working Properly