zoukankan      html  css  js  c++  java
  • Django 如何实现文件下载

    1. 思路:

    文件,让用户下载
        - a标签+静态文件
        - 设置响应头(django如何实现文件下载)

    2. a标签实现

    <a href="/static/xxx.xlsx">下载模板</a>

    3. 设置响应头

    方法一:使用HttpResponse

    from django.shortcuts import HttpResponse  
    def file_down(request):  
        file=open('/home/amarsoft/download/example.tar.gz','rb')  
        response =HttpResponse(file)  
        response['Content-Type']='application/octet-stream'  
        response['Content-Disposition']='attachment;filename="example.tar.gz"'  
        return response 

    方法二:使用StreamingHttpResponse

    from django.http import StreamingHttpResponse  
    def file_down(request):  
        file=open('/home/amarsoft/download/example.tar.gz','rb')  
        response =StreamingHttpResponse(file)  
        response['Content-Type']='application/octet-stream'  
        response['Content-Disposition']='attachment;filename="example.tar.gz"'  
        return response  

    方法三:使用FileResponse

    from django.http import FileResponse  
    def file_down(request):  
        file=open('/home/amarsoft/download/example.tar.gz','rb')  
        response =FileResponse(file)  
        response['Content-Type']='application/octet-stream'  
        response['Content-Disposition']='attachment;filename="example.tar.gz"'  
        return response 

    总结:对比

    虽然使用这三种方式都能实现,但是推荐用FileResponse,在FileResponse中使用了缓存,更加节省资源。虽说是三种方式,但是原理相同,说白了就是一种方式。为了更好的实现文件下载,
    FileResponse对StreamingHttpResponse做了进一步的封装,即StreamingHttpResponse是FileResponse的父类。而HttpResponse,StreamingHttpResponse,
    FileResponse三者都继承了基类HttpResponseBase。HttpResponseBase类是一个字典类,其封装了一个_headers属性,该属性是一个字典类型,里面封装了response的头信息。
    因为该HttpResponseBase类被封装成了一个字典类,所以可以直接使用response['Content-Type']这种形式访问,也可以使用response._headers['Content-Type']访问。值得注意的是: 1.HttpResponseBase只有来设置response的头信息,并不能返回给客户端发生数据。 2.response.keys()这中形式不能访问到字典的方法,必须使用response._headers.keys()才能访问到字典的方法。

    4. 项目案例:

    1.让公司内部可以批量导入客户资源信息;

    2. 首先要下载xlsx模板文件;

    增加URL:

    urlpatterns = [
        url(r'^stark/crm/login/', crm_views.login,name='crm_login'),
        url(r'^stark/crm/index/', crm_views.index,name='crm_index'),
        url(r'^stark/crm/Download/', crm_views.download,name='crm_download'),
    ]

    编写download视图函数:

    def download(request):
        file=open('static/xlsx/xlsx_file.xlsx','rb')
        response =FileResponse(file)
        response['Content-Type']='application/octet-stream'
        response['Content-Disposition']='attachment;filename="xlsx_file.xlsx"'
        return response
    

    前端页面反向解析URL

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>批量导入客户数据</title>
    </head>
    <body>
    
    <h2>批量导入</h2>
    <form action="">
        <a href="{% url 'crm_download' %}">下载模板</a>
        <p><input type="file" name="xsfile"></p>
        <p><input type="submit" value="提交"></p>
    </form>
    
    
    </body>
    </html>

      

     

  • 相关阅读:
    Variable() placeholder() constant() 的区别
    scrapy-redis+selenium+webdriver 部署到linux上
    scrapy-redis+selenium+webdriver解决动态代理ip和user-agent的问题(全网唯一完整代码解决方案)
    [学习记录]NLTK常见操作二(分句,分词,词干提取)
    [学习记录]NLTK常见操作一(去网页标记,统计词频,去停用词)
    [学习记录]python正则表达式
    [学习记录]MySQL之初级查询语句(select,where)
    [学习记录]pymysql的基本操作
    [学习记录]MySQL临时整理
    [学习记录]面对wxpython的长跑(100米:wxpython安装,相关文件,wx.App,wx.Frame)
  • 原文地址:https://www.cnblogs.com/supery007/p/8146035.html
Copyright © 2011-2022 走看看