Python+django实现文件下载
(1)方法一、直接用a标签的href+数据库中文件地址,即可下载。缺点:wordexcel是直接弹框下载,对于imagetxt等文件的下载方式是直接在新页面打开。
(2)方法二、在python后台对下载内容进项处理,返回内容直接弹出下载框。
#后台处理函数
defdownloadFile(req):
filename=basePath+req.GET['url']
deffile_iterator(file_name,chunk_size=512):
withopen(file_name)asf:
whileTrue:
c=f.read(chunk_size)
ifc:
yieldc
else:
break
response=StreamingHttpResponse(file_iterator(filename))
response['Content-Type']='application/octet-stream'
response['Content-Disposition']='attachment;filename="{0}"'.format(filename)
returnresponse
(3)前台使用函数方法
①、a标签调用函数传入路径<ahref='/downloadFile/url=路径'>
②、button标签调用jq方法调用后台函数
<inputtype='button'class='download'>
#下载按钮点击事件
$("body").on("click",".download",function(){3location.href="/downloadFile/?url="+路径;
});
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下