zoukankan      html  css  js  c++  java
  • 2、Python djang 框架下的word Excel TXT Image 等文件的下载

    2、python实现文件下载

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

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

     1 #后台处理函数
     2 def downloadFile(req):
     3     filename=basePath+req.GET['url']
     4     def file_iterator(file_name, chunk_size=512):
     5         with open(file_name) as f:
     6             while True:
     7                 c = f.read(chunk_size)
     8                 if c:
     9                     yield c
    10                 else:
    11                     break
    12     response = StreamingHttpResponse(file_iterator(filename))
    13     response['Content-Type'] = 'application/octet-stream'
    14     response['Content-Disposition'] = 'attachment;filename="{0}"'.format(filename)
    15     return response

    (3)前台使用函数方法

    ①、a标签调用函数传入路径<a href='/downloadFile/url=路径'>

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

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

    1 #下载按钮点击事件
    2 $("body").on("click",".download",function(){3     location.href="/downloadFile/?url="+路径;
    4 });
  • 相关阅读:
    BZOJ#2820. YY的GCD
    BZOJ#3771. Triple(FFT+生成函数)
    BZOJ#4503. 两个串
    HDU#4609. 3-idiots
    UVA 12298 Super Poker II(FFT+母函数)
    HDU#1402. A×B
    树状数组的区间修改与区间查修
    COCI. DIFERENCIJA(序列处理中的小技巧)
    dt.select默认不区分大小写
    缓存,缓存算法和缓存框架
  • 原文地址:https://www.cnblogs.com/hello-word1/p/5136929.html
Copyright © 2011-2022 走看看