栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python-让Django提供可下载文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-让Django提供可下载文件

你可以将

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
标头。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号