发布于 2016-01-18 14:11:44 | 961 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Django教程,程序狗速度看过来!

Django Python WEB开发框架

Django是一个开放源代码的Web应用框架,由Python写成。采用了MVC的软件设计模式,即模型M,视图V和控制器C。它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是CMS(内容管理系统)软件。并于2005年7月在BSD许可证下发布。这套框架是以比利时的吉普赛爵士吉他手Django Reinhardt来命名的。


本文是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="+路径;
 });


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务