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标签调用函数传入路径<a href='/downloadFile/url=路径'>

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

<input type='button' class='download'> 

 #下载按钮点击事件
 $("body").on("click",".download",function(){3   location.href="/downloadFile/?url="+路径;
 });

python结合API实现即时天气信息
python结合API实现即时天气信息importurllib.requestimporturllib.parseimportjson"""利用最美天气抓取即时天气情况http://www.zuimeitianqi.com/"""classZuiMei():def__init__(self):self.

举例讲解Python面相对象编程中对象的属性与类的方法
python对象的属性进入正题,来看一个实例来了解python中类,对象中公有属性,私有属性及局部变量,全局变量的区别.root@10.1.6.200:~#catobject.py#!/usr/bin/envpython#cod

Python使用os模块和fileinput模块来操作文件目录
os模块在python编程中,我们会经常使用到文件和目录,这些操作python提供一个os模块,里面有很多操作文件和目录的函数.下面介绍os的常用功能1.获取当前路