views:

2397

answers:

4

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.

For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt

And on the server, I know that all downloadable files reside in a folder "/home/user/files/".

Is there a way to make Django serve that file for download as opposed to trying to find a URL and View to display it?

+7  A: 

A "download" is simply an HTTP header change.

See http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment for how to respond with a download.

You only need one URL definition for "/download".

The request's GET or POST dictionary will have the "f=somefile.txt" information.

Your view function will simply merge the base path with the "f" value, open the file, create and return a response object. It should be less than 12 lines of code.

S.Lott
+1 For doing the job.
kjfletch
+1  A: 

Django recommend that you use another server to serve static media (another server running on the same machine is fine.) They recommend the use of such servers as lighttp.

This is very simple to set up. However. if 'somefile.txt' is generated on request (content is dynamic) then you may want django to serve it.

Django Docs - Static Files

kjfletch
+15  A: 

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:

response = HttpResponse(mimetype='application/force-download')
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.

elo80ka
If your filename, or path_to_file includes non-ascii characters such as "ä" or "ö", the `smart_str` does not work as intended since apache module X-Sendfile cannot decode the smart_str encoded string. Thus for example "Örinää.mp3" file cannot be served. And if one omits the smart_str, the Django itself throws ascii encoding error because all *headers* are encoded to ascii format before sending. Only way that I know of to circumvent this problem is to reduce X-sendfile filenames to ones that consists only ascii.
Ciantic
A: 

Is there any way to return a file and a redirect?

Ideally:

1) The user would click a link

2) A dynamically generated file would download

3) The user would be redirected to another page

Thanks

Daniel
You shouldn't ask questions in answers. Search if your question was already asked and, if it wasn't, ask it yourself as a separate question.
Catalin Iacob