Python Django搭建文件下载服务器的实现
作者:拉灯的小手 时间:2021-06-04 14:37:55
环境
win10
Python:3.6.7
Django:2.2.7
运行效果
1、创建 Django 项目
# 创建Download项目
django-admin startproject Download
# 创建down_app app
python manage.py startapp down_app
2、修改配置文件:settings.py
Download/Download/settings.py
1.添加注册APP:down_app
2.设置模板文件路径:templates
3、编写视图函数:views.py
Download/down_app/views.py
import os
from django.http import HttpResponse
from django.http import StreamingHttpResponse
def image_down(request):
"""
下载图片
"""
img_name = request.GET.get("username") + ".png" # 二维码图片名
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 项目根目录
file_path = os.path.join(base_dir, 'antirisk/CodeGenerate/image/code', img_name) # 二维码的绝对路径
if not os.path.isfile(file_path): # 判断下载文件是否存在
return HttpResponse("Sorry but Not Found the File")
def file_iterator(file_path, chunk_size=512):
"""
文件生成器,防止文件过大,导致内存溢出
:param file_path: 文件绝对路径
:param chunk_size: 块大小
:return: 生成器
"""
with open(file_path, mode='rb') as f:
while True:
c = f.read(chunk_size)
if c:
yield c
else:
break
try:
# 设置响应头
# StreamingHttpResponse将文件内容进行流式传输,数据量大可以用这个方法
response = StreamingHttpResponse(file_iterator(file_path))
# 以流的形式下载文件,这样可以实现任意格式的文件下载
response['Content-Type'] = 'application/octet-stream'
# Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名
response['Content-Disposition'] = f'attachment;filename="1.png"' # 文件名不可设置为中文
except:
return HttpResponse("Sorry but Not Found the File")
return response
4、修改路由配置:urls.py
Download/Download/urls.py
from django.contrib import admin
from django.urls import path, re_path
from down_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
re_path('download/)', views.image_down, name="download"),
]
5、创建并编写:index.html
Download/templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/download/" rel="external nofollow" >下载图片</a>
</body>
</html>
运行
# 运行项目
python manage.py runserver
# 访问: http://127.0.0.1:8000/
来源:https://blog.csdn.net/qq_38154948/article/details/112508229
标签:Django,服务器,搭建
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在VScode里面添加Python解释器的详细步骤
2021-12-04 16:03:11
![](https://img.aspxhome.com/file/2023/7/100817_0s.png)
Python之基础函数案例详解
2023-07-09 19:43:24
![](https://img.aspxhome.com/file/2023/8/87808_0s.png)
Symfony模板的快捷变量用法实例
2023-11-15 04:43:24
无级分类的多级联动
2020-07-02 12:53:12
MySQL Order By语法
2011-01-04 19:58:00
用PHP实现标准的IP Whois查询
2023-11-14 19:35:01
浅谈Python列表嵌套字典转化的问题
2023-12-11 10:37:43
![](https://img.aspxhome.com/file/2023/5/93785_0s.jpg)
Python 调用VC++的动态链接库(DLL)
2023-06-19 09:01:44
Python 多进程和数据传递的理解
2021-06-01 02:30:07
ASP使用fso遍历文件及文件夹列出文件名
2008-10-27 19:32:00
Python基础面向对象之继承与派生详解
2022-04-20 11:58:53
![](https://img.aspxhome.com/file/2023/4/67424_0s.png)
python正则表达式中匹配次数与贪心问题详解(+ ?*)
2021-04-24 20:52:04
![](https://img.aspxhome.com/file/2023/6/82686_0s.png)
老生常谈CSS网页布局的意义与副作用
2008-09-12 12:31:00
Python性能分析工具py-spy原理用法解析
2021-08-30 14:57:23
![](https://img.aspxhome.com/file/2023/1/89441_0s.png)
python中使用ctypes调用so传参设置遇到的问题及解决方法
2021-06-02 00:38:39
Python urllib.request对象案例解析
2022-04-14 22:24:43
opencv用VS2013调试时用Image Watch插件查看图片
2022-04-20 14:49:12
![](https://img.aspxhome.com/file/2023/9/90369_0s.png)
Python类装饰器实现方法详解
2021-11-15 20:01:05
用JAVASCRIPT格式化数字成货币(逗号隔开)
2008-01-30 12:34:00
关注oblog 关注xml-rpc 自己开发客户端小记
2009-10-04 20:27:00