Django with mod_XSENDFILE cannot load complete file

Attached code which downloads file from browser using django 1.3 and Apache 2.2 with mod_xsendfile

@login_required
def sendfile(request, productid):       
    path = settings.RESOURCES_DIR
    filepath = os.path.join('C:/workspace/y/src/y/media/audio/','sleep_away.mp3')  
    print "filepath",filepath  
    filename = 'sleep_away.mp3' # Select your file here.   
    print "Within sendfile size", os.path.getsize(filepath)
    wrapper = FileWrapper(open(filepath,'r'))     
    content_type = mimetypes.guess_type(filename)[0]     
    response = HttpResponse(wrapper, content_type = content_type) 
    print "Within wrapper"
    from django.utils.encoding import smart_str
    response['X-Sendfile'] = smart_str(filepath)
    response['Content-Length'] = os.path.getsize(filepath) 
    from django.utils.encoding import  smart_str    
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(filename)      
    return response 

      

The console displays the following file size which is the correct size Within the upload file size 4842585

But when I load / save the file it shows 107KB ... i.e. 109,787 bytes. Where am I going wrong. Why doesn't it download the complete file?

+2


a source to share


2 answers


I consider yours new to django or python. Try putting statements import

at the beginning method

. Once imported, it can be used through a method that doesn't need to be imported every time you use. On windows, you have to use "rb"

(read binary) to serve up anything but text files. Avoid using variable names that might conflict with method names or other language keywords. Your method should look like this:

@login_required
def sendfile(request, productid):
    from django.utils.encoding import smart_str

    ##set path and filename
    resource_path = settings.RESOURCES_DIR # resource dir ie /workspace/y/src/y/media
    filename = "sleep_away.mp3" #file to be served 

    ##add it to os.path  
    filepath = os.path.join(resource_path,"audio",filename)
    print "complete file path: ", filepath     

    ##filewrapper to server in size of 8kb each until whole file is served   
    file_wrapper = FileWrapper(file(filepath,'rb')) ##windows needs rb (read binary) for non text files   

    ##get file mimetype
    file_mimetype = mimetypes.guess_type(filepath)

    ##create response with file_mimetype and file_wrapper      
    response = HttpResponse(content_type=file_mimetype, file_wrapper)

    ##set X-sendfile header with filepath
    response['X-Sendfile'] = filepath ##no need for smart_str here.

    ##get filesize
    print "sendfile size", os.stat(filepath).st_size
    response['Content-Length'] = os.stat(filepath).st_size ##set content length    
    response['Content-Disposition'] = 'attachment; filename=%s/' % smart_str(filename) ##set disposition     

    return response ## all done, hurray!! return response :)

      



Hope it helps

+2


a source


You can take a look at the django-private-files project. I haven't tested it myself, but it looks promising. docs link -> http://readthedocs.org/docs/django-private-files/en/latest/usage.html



amuses

0


a source







All Articles