你可以将
S.Lott的解决方案与
xsendfile模块结合使用:
django生成文件(或文件本身)的路径,但是实际的文件服务由
Apache / Lighttpd处理。设置
mod_xsendfile后,与视图集成将需要几行代码:
from django.utils.encoding import smart_strresponse = HttpResponse(mimetype='application/force-download') # mimetype is replaced by content_type for django 1.7response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)response['X-Sendfile'] = smart_str(path_to_file)# It's usually a good idea to set the 'Content-Length' header too.# You can also set any other required headers: Cache-Control, etc.return response
当然,只有在你可以控制服务器或托管公司已经设置了
mod_xsendfile的情况下,这才起作用。
编辑:
django 1.7将mimetype替换为
content_type
response = HttpResponse(content_type='application/force-download'
编辑: 对于
nginx检查此,它使用
X-Accel-Redirect而不是
apacheX-Sendfile标头。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)