Django与mod_XSENDFILE无法下载完整的文件 [英] Django with mod_XSENDFILE unable to download complete file

查看:435
本文介绍了Django与mod_XSENDFILE无法下载完整的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加的是使用django 1.3和Apache 2.2与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')
打印filepath,文件路径
filename ='sleep_away.mp3'#在此选择您的文件。
print在sendfile大小内,os.path.getsize(filepath)
wrapper = FileWrapper(open(filepath,'r'))
content_type = mimetypes.guess_type(filename)[0 ]
response = HttpResponse(wrapper,content_type = content_type)
打印包装内
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'] ='附件; filename =%s /'%smart_str(filename)
返回响应

控制台显示以下文件大小是正确的大小
在sendfile大小4842585



但是当我下载/保存文件它显示107 KB ...即109,787个字节我错了为什么不下载完整的文件?

解决方案

我认为你的新的django或python。尝试将导入语句放在方法开头。一旦导入就可以通过方法使用,每次使用时都不需要导入。在Windows中,您应该使用rb(读取二进制文件)来提供文本文件以外的任何东西。尽量不要使用可能与方法名称或语言的其他关键字冲突的变量名。你的方法应该像这样

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

##设置路径和文件名
resource_path = settings.RESOURCES_DIR#resource dir ie / workspace / y / src / y / media
filename = sleep_away.mp3#file要提供

##将其添加到os.path
filepath = os.path.join(resource_path,audio,filename)
print 完整的文件路径:,文件路径

##文件包装到大小为8kb的服务器,直到整个文件被提供
file_wrapper = FileWrapper(file(filepath,'rb'))## Windows需要非文本文件的rb(读取二进制)

##获取文件mimetype
file_mimetype = mimetypes.guess_type(filepath)

##使用file_mimetype创建响应和file_wrapper
response = HttpResponse(content_type = file_mimetype,file_wrapper)

##设置X-sendfile标题机智h文件路径
响应['X-Sendfile'] =文件路径##不需要smart_str这里。

## get filesize
printsendfile 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 !!返回响应:)

希望有帮助


Attached is the code which downloads a 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 shows the following filesize which is the right size Within sendfile size 4842585

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

解决方案

I consider your new to django or python. Try to put the import statements at the beginning of the method. Once imported it can be used through the method no need import every time you use. In windows you should use "rb" (read binary) to serve anything other than text files. Try not to use variable names that might conflict with method names or other keywords of the language. Your method should be 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 that helps

这篇关于Django与mod_XSENDFILE无法下载完整的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆