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!

dana
  • 5,168
  • 20
  • 75
  • 116

3 Answers3

2

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
  • 68,631
  • 21
  • 159
  • 205
  • hi, sry for delaying. i've edited the new code- works, but it actually downloads my script:D because of that __file__ of course. What should i replace that __file__ with for the download to be right? thanks! – dana Jun 30 '10 at 16:59
  • 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 Jun 30 '10 at 18:03
  • 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 Jun 30 '10 at 18:15
  • 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 Jun 30 '10 at 19:14
2

I answered this question here, hope it helps.

Community
  • 1
  • 1
cji
  • 6,635
  • 2
  • 20
  • 16
  • 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 Jul 09 '10 at 15:54
1

Try one of these approaches:

1) Disable GZipMiddleware if you are using it;

2) Apply a patch to django/core/servers/basehttp.py described in https://code.djangoproject.com/ticket/6027

Hanson
  • 254
  • 2
  • 10