文件上传的两种方案

root
abc abc
  • 16 Jul

文件上传
1,使用自带的FileSystemStorage类
简单功能实现

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函数
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": "上传成功"})

原文链接:https://blog.csdn.net/qq_37...