Search code examples
djangofiledownloadsendfile

Django download file empty


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!


Solution

  • I answered this question here, hope it helps.