Django download file empty

做~自己de王妃 提交于 2019-12-06 12:01:19
cji

I answered this question here, hope it helps.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!