Django Zip文件下载
Django Zip
本文讲述通过Rest-API方式下载zip文件
1.运行环境
a)Django 1.11.20
b)python 3.7.1 x64
c)djangorestframework 3.11.0
2.流程分析
首先zip文件是不需要提前存在的,用户可以选择将哪些文件打包成zip文件,然后下载,可以采用两种方式
1)待打包文件放入memory中
2)打包文件放到disk
对于大文件我们可以采用2)的方式,不占用太多资源
3.用到的module
a)FileWrapper
b)tempfile
c)zipfile
4.Code
from rest_framework.generics import GenericAPIView
from django.http import HttpResponse
from wsgiref.util import FileWrapper
# from django.core.servers.basehttp import FileWrapper
import tempfile
import zipfileclass DownLoadZipFile(GenericAPIView):def __init__(self):super(GenericAPIView, self).__init__()def post(self, request):"""API 'POST' method for send command operation:param request::return response:"""# for testfile_path_list = [{'file_name': 'file1', 'file_path': 'full_path'}]temp = tempfile.TemporaryFile()archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)for file_path_dict in file_path_list:file_path = file_path_dict.get('file_path', None)file_name = file_path_dict.get('file_name', None)archive.write(file_path, file_name) # TODO need check file exist or notarchive.close()lenth = temp.tell()temp.seek(0)wrapper = FileWrapper(temp)# Using HttpResponseresponse = HttpResponse(wrapper, content_type='application/zip')response['Content-Disposition'] = 'attachment; filename=archive.zip'response['Content-Length'] = lenth # temp.tell()return response
5.说明
from wsgiref.util import FileWrapper
# from django.core.servers.basehttp import FileWrapper
Django版本不同可能import方式不同,对于我们的环境上面一条导入成功。
temp = tempfile.TemporaryFile()
会在C:\Users\<username>\AppData\Local\Temp\ 下创建临时文件
至于seek/tell用法,可参考seek/tell
Django Zip文件下载相关推荐
- SpringBoot实现zip文件下载
Controller实现zip文件下载 public void download(String tableName) {ByteArrayOutputStream byteArrayOutputStr ...
- Python Django 搭建文件下载服务器
本文仅供学习交流使用,如侵立删!demo下载见文末 Python Django 搭建文件下载服务器 环境 win10 Python:3.6.7 Django:2.2.7 运行效果 1.创建 Djang ...
- Python/Django 大文件下载问题
最近使用Python/Django开发一个系统,暂且称之为A系统,卡在大文件下载上面,查询了django官方doc和stackoverflow上的很多资料,最终圆满解决此问题,如下为具体过程: 一 A ...
- Django的文件下载
在实际的项目中很多时候需要用到下载功能,如导excel.pdf或者文件下载,当然你可以使用web服务自己搭建可以用于下载的资源服务器,如nginx,这里我们主要介绍django中的文件下载. 我们这里 ...
- java 导出表格打包zip文件下载_asyExcel导出excel并打包成zip压缩包下载
假期期间自己在家撸码,刚好用到了导出,导出来之后是多个文件,所以需要打成压缩包并下载来给客户.查阅了一些资料,把这段代码贴在这,相当于有个记录吧. package com.business.testE ...
- django 压缩文件下载
简介 需求: 需要在请求时,将数据生成zip文件提供给用户下载. 不想要在生成后再提供给用户下载 解决: 使用BytesIO在内存中写入数据,而不是落地到本地中. 栗子 from io import ...
- Axios实现zip文件下载
前端代码片段 - zipDownload.js import axios from 'axios' import baseURL from './baseUrl'const mimeMap = {xl ...
- java 导出表格打包zip文件下载_POI多个工作簿导出表格打包ZIP下载
首先获得workbook集合对象 public static void zipFiles(List srcfile, File zipfile,String fileName) { try { Zip ...
- Android zip文件下载和解压
下载: DownLoaderTask.java package com.johnny.testzipanddownload;import java.io.BufferedInputStream; im ...
- zip 文件下载函数封装
代码: export default {/*** 下载文件*/exportFile: (fileUrl) => {if (fileUrl) {let exportFrame = document ...
最新文章
- PHP合并数组array_merge函数运算符加号与的区别
- Spring的核心思想,总结得非常好!
- powerBi odbc 连接impala 实现自助分析
- MATLAB错误:‘conv2’
- 前端与移动开发之vue-day1(1)
- Android 学习心得(2)——Android内置数据库SQLite
- 软件工程习题2第三小题
- 智能高柜机器人_再装修说啥也不能忘,橱柜旁边预留50公分,打高柜能多个家政间...
- BZOJ 3166 set+可持久化trie树(OR 莫队)
- HTML代码页面无法跳转为什么,html跳转新页面代码_html页面跳转代码
- 《普林斯顿微积分读本》笔记-第2章三角学回顾
- 修改oracle用户资源限制,oracle用户登陆失败次数限制修改
- 只因一段代码全公司200多人被捕,爬虫敲响警钟!
- Airbnb短租数据分析报告
- 神马笔记 版本1.8.0——删除笔记/文件夹·代码篇
- OCR文本识别,文本行检测技术。
- css html 鼠标手型,鼠标形状,鼠标效果,样式
- GridView动态添加一编辑行
- 免费全国车辆违章查询API接口及其使用方法
- MATLAB Signal Rrocessing(13)视频和音频