加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

django三种文件下载方式

发布时间:2020-12-15 17:17:10 所属栏目:大数据 来源:网络整理
导读:table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0" tr tdspan style="font-size: 16px;" 一、概述 /td /tr /table 在实际的项目中很多时候需要用到下载功能,如导excel、pdf或者文件下载,当然你可以使用
<tr>
<td><span style="font-size: 16px;">一、概述</td>
</tr></table>

在实际的项目中很多时候需要用到下载功能,如导excel、pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍django中的文件下载。

实现方式:a标签+响应头信息(当然你可以选择form实现)

<点我下载

<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">

<tr>
<td><span style="font-size: 16px;">方式一:使用HttpResponse</td>
</tr></table>

路由url:

url(r,views.download,name=),

views.py代码

django.shortcuts = open(,=] = response[] = response

<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">

<tr>
<td><span style="font-size: 16px;">方式二:使用StreamingHttpResponse</td>
</tr></table>

其他逻辑不变,主要变化在后端处理

django.http =open(,=]=]= response

<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">

方式三:使用FileResponse django.http =open(,=]=]= response

<table style="height: 30px; background-color: #afeeee; width: 1266px; ; width: 1266px;" border="0">

<tr>
<td><span style="font-size: 16px;">使用总结</td>
</tr></table>

三种http响应对象在django官网都有介绍.入口:https://docs.djangoproject.com/en/1.11/ref/request-response/

推荐使用FileResponse,从源码中可以看出FileResponse是StreamingHttpResponse的子类,内部使用迭代器进行数据流传输。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读