views:

54

answers:

2

I am writing a simple function for downloading a certain file, from the server, to my machine. The file is unique represented by its id. The file is locatd corectly, and the download is done, but the downloaded file (though named as the one on the server) is empty. my download function looks like this:

def download_course(request, id):
    course = Courses.objects.get(pk = id).course
    path_to_file = 'root/cFolder'
    filename = __file__ # Select your file here.                                
    wrapper = FileWrapper(file(filename))
    content_type = mimetypes.guess_type(filename)[0]
    response = HttpResponse(wrapper, content_type = content_type)
    response['Content-Length'] = os.path.getsize(filename)
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(course)

    return response

where can i be wrong? thanks!

+1  A: 

Looks like you're not sending any data (you don't even open the file).

Django has a nice wrapper for sending files (code taken from djangosnippets.org):

def send_file(request):
    """                                                                         
    Send a file through Django without loading the whole file into              
    memory at once. The FileWrapper will turn the file object into an           
    iterator for chunks of 8KB.                                                 
    """
    filename = __file__ # Select your file here.                                
    wrapper = FileWrapper(file(filename))
    response = HttpResponse(wrapper, content_type='text/plain')
    response['Content-Length'] = os.path.getsize(filename)
    return response

so you could use something like response = HttpResponse(FileWrapper(file(path_to_file)), mimetype='application/force-download').

If you are really using lighttpd (because of the "X-Sendfile" header), you should check the server and FastCGI configuration, I guess.

AndiDog
hi, sry for delaying. i've edited the new code- works, but it actually downloads my script:Dbecause of that __file__ of course. What should i replace that __file__ with for the download to be right? thanks!
dana
Well, `__file__` is of course the Python script filename. You need to replace it with the filename of the file that should be downloaded (something that has to do with courses, I guess).
AndiDog
hmm.. i guessed so, and i've replaced it with course, where course is the course for the link course = Courses.objects.get(pk = id).course but my error is: coercing to Unicode: need string or buffer, FieldFile found
dana
In my example, you have to provide a filename. If you really have a `FileField`, `course` will be a file-like object (http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield-and-fieldfile) and you can replace `file(filename)` in my example with `course.open()`..open
AndiDog
+1  A: 

I answered this question here, hope it helps.

cji
this answer helped me! (the bounty will be given in 22 hours - as i;ve started it today, and can accept an answer in at least 22 hrs)
dana