Question

I'm trying to create a simple way of serving downloadable content with Django. The idea is that logged in users shall be able to download (rather large) files through lighttpd.

There are several posts about this here on SO and I came also across a blog post with a simple solution.

I created a view as in the abovementioned link (and added "allow-x-send-file" => "enable" to the lighttpd config), and it "works" sort of. When I check headers with Firebug I get the correct content type, file length and 200 OK, but no file is downloaded.

Then I found a solution here on SO, where additional headers are sent. Now a file is served, but the downloaded file is empty. Headers are still correct.

Here's my source (with removed auth_decorators and no handling of non-existent file):

import os
import mimetypes
import django.http

from django.conf import settings

def get_absolute_filename(filename='', safe=True):
    if not filename:
        return os.path.join(settings.FILE_DOWNLOAD_PATH, 'index')
    if safe and '..' in filename.split(os.path.sep):
        return get_absolute_filename(filename='')
    return os.path.join(settings.FILE_DOWNLOAD_PATH, filename)

def retrieve_file(request, filename=''):
    abs_filename = get_absolute_filename(filename)
    response = django.http.HttpResponse(mimetype='application/force-download')
    response['X-Sendfile'] = abs_filename
    response['Content-Disposition'] = 'attachment; filename=%s' % abs_filename
    response['Content-Type'] = mimetypes.guess_type(abs_filename)
    response['Content-Length'] = os.path.getsize(abs_filename)
    return response
Was it helpful?

Solution

Look at your source - you send no file, only headers.

OTHER TIPS

Pre-1.5 versions of lighttpd use the X-LIGHTTPD-send-file header instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top