Django实现文件上传和下载功能


Posted in Python onOctober 06, 2019

本文实例为大家分享了Django下完成文件上传和下载功能的具体代码,供大家参考,具体内容如下

一、文件上传

Views.py

def upload(request):
 if request.method == "POST": # 请求方法为POST时,进行处理
 myFile = request.FILES.get("myfile", None) # 获取上传的文件,如果没有文件,则默认为None
 if not myFile:
 return HttpResponse("no files for upload!")
 # destination=open(os.path.join('upload',myFile.name),'wb+')
 destination = open(
 os.path.join("你的文件存放地址", myFile.name),
 'wb+') # 打开特定的文件进行二进制的写操作
 for chunk in myFile.chunks(): # 分块写入文件
 destination.write(chunk)
 destination.close()
 return HttpResponse("upload over!")
 else:
 file_list = []
 files = os.listdir('D:\python\Salary management system\django\managementsystem\\file')
 for i in files:
 file_list.append(i)
 return render(request, 'upload.html', {'file_list': file_list})

urls.py

url(r'download/$',views.download),

upload.html

<div class="container-fluid">
 <div class="row">
 <form enctype="multipart/form-data" action="/upload_file/" method="POST">
 <input type="file" name="myfile"/>
 <br/>
 <input type="submit" value="upload"/>
 </form>
 </div>
</div>

 页面显示

 Django实现文件上传和下载功能

二、文件下载

Views.py

from django.http import HttpResponse,StreamingHttpResponse
from django.conf import settings
 
def download(request):
 filename = request.GET.get('file')
 filepath = os.path.join(settings.MEDIA_ROOT, filename)
 fp = open(filepath, 'rb')
 response = StreamingHttpResponse(fp)
 # response = FileResponse(fp)
 response['Content-Type'] = 'application/octet-stream'
 response['Content-Disposition'] = 'attachment;filename="%s"' % filename
 return response
 fp.close()

HttpResponse会直接使用迭代器对象,将迭代器对象的内容存储城字符串,然后返回给客户端,同时释放内存。可以当文件变大看出这是一个非常耗费时间和内存的过程。

而StreamingHttpResponse是将文件内容进行流式传输,StreamingHttpResponse在官方文档的解释是:

The StreamingHttpResponse class is used to stream a response from Django to the browser. You might want to do this if generating the response takes too long or uses too much memory.

这是一种非常省时省内存的方法。但是因为StreamingHttpResponse的文件传输过程持续在整个response的过程中,所以这有可能会降低服务器的性能。

urls.py

url(r'^upload',views.upload),

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之关于循环的小伎俩
Oct 02 Python
Python中threading模块join函数用法实例分析
Jun 04 Python
Flask框架的学习指南之用户登录管理
Nov 20 Python
Python写的一个定时重跑获取数据库数据
Dec 28 Python
在 Python 应用中使用 MongoDB的方法
Jan 05 Python
python数据结构之列表和元组的详解
Sep 23 Python
Python排序算法之选择排序定义与用法示例
Apr 29 Python
Python匿名函数/排序函数/过滤函数/映射函数/递归/二分法
Jun 05 Python
python如何编写win程序
Jun 08 Python
Python 利用argparse模块实现脚本命令行参数解析
Dec 28 Python
Pycharm 设置默认解释器路径和编码格式的操作
Feb 05 Python
python关于集合的知识案例详解
May 30 Python
Django文件上传与下载(FileFlid)
Oct 06 #Python
Django实现文件上传下载
Oct 06 #Python
python编写猜数字小游戏
Oct 06 #Python
python实现猜数字游戏
Mar 25 #Python
python实现机器人卡牌
Oct 06 #Python
Django实现文件上传下载功能
Oct 06 #Python
使用turtle绘制五角星、分形树
Oct 06 #Python
You might like
Terran建筑一览
2020/03/14 星际争霸
PHP 5.0对象模型深度探索之类的静态成员
2008/03/27 PHP
跟我学Laravel之路由
2014/10/15 PHP
PHP使用NuSOAP调用Web服务的方法
2015/07/18 PHP
php使用CutyCapt实现网页截图保存的方法
2016/10/03 PHP
ecshop添加菜单及权限分配问题
2017/11/21 PHP
详解no input file specified 三种解决方法
2019/11/29 PHP
3款实用的在线JS代码工具(国外)
2012/03/15 Javascript
window.showModalDialog参数传递中含有特殊字符的处理方法
2013/06/06 Javascript
通过Javascript读取本地Excel文件内容的代码示例
2014/04/08 Javascript
JavaScript获取flash对象与网上的有所不同
2014/04/21 Javascript
jQuery中的编程范式详解
2014/12/15 Javascript
JS读写CSS样式的方法汇总
2016/08/16 Javascript
jQuery+ThinkPHP+Ajax实现即时消息提醒功能实例代码
2017/03/21 jQuery
小程序实现带年月选取效果的日历
2018/06/27 Javascript
JavaScript函数式编程(Functional Programming)箭头函数(Arrow functions)用法分析
2019/05/22 Javascript
bootstrap table实现横向合并与纵向合并
2019/07/18 Javascript
[28:57]EG vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/16 DOTA
[01:38]女王驾到——至宝魔廷新尊技能&特效展示
2020/06/16 DOTA
常用python数据类型转换函数总结
2014/03/11 Python
urllib和BeautifulSoup爬取维基百科的词条简单实例
2018/01/17 Python
python中eval与int的区别浅析
2019/08/11 Python
Python 网络编程之TCP客户端/服务端功能示例【基于socket套接字】
2019/10/12 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
2020/01/14 Python
Python 格式化输出_String Formatting_控制小数点位数的实例详解
2020/02/04 Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
2020/04/23 Python
sklearn和keras的数据切分与交叉验证的实例详解
2020/06/19 Python
Python3爬虫mitmproxy的安装步骤
2020/07/29 Python
时装界的“朋克之母”:Vivienne Westwood
2017/07/06 全球购物
什么是数据抽象
2016/11/26 面试题
JDBC操作数据库的基本流程是什么
2014/10/28 面试题
七一党建活动方案
2014/01/28 职场文书
寄语十八大感言
2014/02/07 职场文书
人事部岗位职责范本
2014/03/05 职场文书
房产授权委托书范本
2014/09/22 职场文书
2015年小学语文教师工作总结
2015/10/23 职场文书