python下载django,Python+django实现文件下载

(1)方法一、直接用a标签的href+数据库中文件地址,即可下载。缺点:word excel是直接弹框下载,对于image txt 等文件的下载方式是直接在新页面打开。

(2)方法二、在python后台对下载内容进项处理,返回内容直接弹出下载框。

#后台处理函数

def downloadFile(req):

filename=basePath+req.GET['url']

def file_iterator(file_name, chunk_size=512):

with open(file_name) as f:

while True:

c = f.read(chunk_size)

if c:

yield c

else:

break

response = StreamingHttpResponse(file_iterator(filename))

response['Content-Type'] = 'application/octet-stream'

response['Content-Disposition'] = 'attachment;filename="{0}"'.format(filename)

return response

(3)前台使用函数方法

①、a标签调用函数传入路径

②、button标签调用jq方法调用后台函数

#下载按钮点击事件

$("body").on("click",".download",function(){3 location.href="/downloadFile/?url="+路径;

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值