Django实现文件上传和下载功能
作者:python_voice 时间:2022-04-02 15:19:34
本文实例为大家分享了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>
页面显示
二、文件下载
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),
来源:https://blog.csdn.net/qq_38332436/article/details/90139229
标签:Django,上传,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python爬虫基础之爬虫的分类知识总结
2021-12-24 03:20:12
![](https://img.aspxhome.com/file/2023/4/71464_0s.png)
Photoshop设计制作网站流程图解
2007-10-25 12:06:00
![](https://img.aspxhome.com/file/UploadPic/200711/3/2007113203253675s.gif)
SQLServer中数据库文件的存放方式,文件和文件组
2012-01-05 18:56:33
楼层数横排比竖排好
2008-04-26 07:28:00
![](https://img.aspxhome.com/file/UploadPic/20084/26/200842673425574s.jpg)
IE中选择符的4095限制
2009-10-09 13:25:00
php小技巧之过滤ascii控制字符
2023-10-03 05:13:15
JavaScript 数组的 uniq 方法
2007-12-07 18:28:00
Frontpage中网页字体的美化研究
2008-03-10 12:13:00
oracle 服务启动,关闭脚本(windows系统下)
2009-07-26 08:57:00
用户反馈对产品设计的帮助
2009-02-09 13:15:00
![](https://img.aspxhome.com/file/UploadPic/20092/9/blog02081-75s.jpg)
关于jsp版ueditor1.2.5的部分问题解决(上传图片失败)
2023-06-15 06:45:00
一文搞懂Go Exec 僵尸与孤儿进程
2023-10-21 07:14:07
Python批量生成幻影坦克图片实例代码
2021-04-17 03:16:17
![](https://img.aspxhome.com/file/2023/3/81473_0s.png)
破解空间实现域名绑定到子目录方法
2010-03-14 11:29:00
php实现微信公众号主动推送消息
2023-11-16 15:10:31
Django--权限Permissions的例子
2021-02-16 01:44:51
在Spring中用select last_insert_id()时遇到问题
2009-05-24 19:50:00
python 中文编码乱码问题的解决
2021-03-06 16:43:58
![](https://img.aspxhome.com/file/2023/5/78645_0s.png)
javascript面向对象三大特征之封装实例详解
2023-08-23 21:39:04
简单了解python的break、continue、pass
2022-06-01 01:20:10
![](https://img.aspxhome.com/file/2023/5/76975_0s.png)