Django实现文件上传及下载的方式,FileSystemStorage和StreamingHttpResponse的使用

文件保存的路径配置

python 复制代码
import platform

system = platform.system()  # 获取当前系统类型
if system == "Windows":  # 测试环境
    MEDIA_ROOT = "F:/Download/media/"
    DOMAIN = "http://127.0.0.1/"
elif system == "Linux":  # 线上环境
    MEDIA_ROOT = "/mnt/media/"
    DOMAIN = "http://xxxx.com/"
else:
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
    DOMAIN = "http://127.0.0.1/"

# print(MEDIA_ROOT)

MEDIA_URL = '/media/'

一、文件上传

1,使用自带的FileSystemStorage类

简单功能实现

python 复制代码
from django.views import View
from django.core.files.storage import FileSystemStorage


class SingleMediaView(View):
    def post(self, request):
        file = request.FILES.get('file', None)
        # 保存上传的文件到指定的绝对目录
        fs = FileSystemStorage()
        real_path = settings.MEDIA_ROOT + format_filename
        fs.save(real_path, file)
        return JsonResponse({"status": 200, "msg": "上传成功"})
2,使用open函数
python 复制代码
from django.views import View


class SingleMediaView(View):
    def post(self, request):
        file = request.FILES.get('file', 'None')
       
        with open('path/file.txt', 'wb+') as destination:
            for chunk in file.chunks():
                destination.write(chunk)
        return JsonResponse({"status": 200, "msg": "上传成功"})

open函数的第一方法写具体保存文件的路径

二、下载文件

使用StreamingHttpResponse类

python 复制代码
class DownloadFileView(View):
    """文件下载"""
    def get(self, request):
        file_id = request.GET.get("file_id", None)  # 文件id

        if file_id:
            media = Media.objects.get(id=file_id)  # 查询数据库
            real_path = settings.MEDIA_ROOT + media.url  # 获取文件路径

            # 使用StreamingHttpResponse进行大文件下载,不占内存
            def file_iterator(file_path, chunk_size=8192):
                with open(file_path, 'rb') as file:
                    while True:
                        data = file.read(chunk_size)
                        if not data:
                            break
                        yield data

            file_format = media.format
            response = StreamingHttpResponse(file_iterator(real_path))
            if media.type == 1:  # 图片下载
                response['Content-Type'] = f'image/{file_format}'
            elif media.type == 2:  # 视频下载
                response['Content-Type'] = f'video/{file_format}'
            elif media.type == 3:  # 其他类型文件下载
                response['Content-Type'] = 'application/octet-stream'

            # 使用quote对下载的中文文件名进行url编码,否则在Content-Disposition里的filename无法正确识别
            encoded_string = quote(f'{media.title}.{file_format.lower()}')
            response['Content-Disposition'] = f'attachment; filename="{encoded_string}"'

            return response

为DownloadFileView类视图配置url后请求时就可以直接下载了

相关推荐
Via_Neo1 分钟前
不能对方法返回值进行赋值
开发语言·python
m0_743623921 分钟前
Tailwind CSS如何实现鼠标悬停变色_使用hover-bg-blue-500类
jvm·数据库·python
2301_777599372 分钟前
CSS如何实现复杂的边框渐变效果_配合border-image使用
jvm·数据库·python
HHHHH1010HHHHH3 分钟前
SQL高效实现基于JOIN的交叉分析_多表关联实现多维统计
jvm·数据库·python
m0_515098424 分钟前
SSD硬盘对HTML工具速度有影响吗_存储介质与开发效率关系【详解】
jvm·数据库·python
weixin_568996064 分钟前
Bootstrap中.d-none类在不同分辨率下的高级用法
jvm·数据库·python
qq_206901394 分钟前
golang如何调用Twilio语音短信API_golang Twilio语音短信API调用实战
jvm·数据库·python
m0_684501985 分钟前
c++如何将宽字符串wstring输出到UTF-8文件_C++17编码转换【附源码】
jvm·数据库·python
IT_陈寒5 分钟前
Vue的响应式更新把我坑惨了,原来问题出在这里
前端·人工智能·后端
智慧地球(AI·Earth)6 分钟前
用 Python 构建一个“记性好”的 AI 助手:JSON本地存储和向量检索
人工智能·python·json