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 
    复制代码

    总结:对比

    1
    2
    3
    虽然使用这三种方式都能实现,但是推荐用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视图函数:

    1
    2
    3
    4
    5
    6
    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>
    复制代码
  • 相关阅读:
    js中给一个元素添加事件
    asp.net客户端回调
    (转)web元素之input (javascript)功能
    在wcf中使用基于证书的授权和保护
    (转)IE与FireFox的js和css几处不同点
    IE&FF兼容性问题
    solaris UFS文件系统 要点
    perl命令行 代替 各种shell 一则
    nginx中的验证模块
    漂亮的正则,素数查找
  • 原文地址:https://www.cnblogs.com/baili-luoyun/p/11557548.html
Copyright © 2011-2022 走看看