【Django组件】UNIAPP+DJANGO 多图像上传 + 后端接口接受 uView组件(转载)

VUE文件:


后端接口文件

class OrderSubmit(APIView):

    def post(self, request):

         # 订单图片对象
          file_obj = request.FILES.getlist('images', None)
          
          function = "submit_order"

         # 上传图片  轮子写在下面
         status, path = upload_files.UploadFilesOnce(request.user, function, file_obj)

        if status == False:
            message["code"] = 10041
            message["message"] = "图片上传失败"
            return JsonResponse(message)

单个 上传轮子文件

def UploadFilesOnce(username,function,files):
    try:

        # 上传图片路径    media/username/task_id/images.jpg
        dir_path = BASE_DIR + "/media/{}/{}/".format(function,username)
        # 如果没有这个路径就创建
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

        path=''
        # 上传图片
        for f in files:
            # 为了避免覆盖 对名字进行加密
            image_name = datamd5.md5(f.name) + '.jpg'
            with open(dir_path + image_name, 'wb') as dest:
                # 获取路径
                path = path + '/media/{}/{}/'.format(function,username) + image_name + ","
                for chunk in f.chunks():
                    dest.write(chunk)

        return True,path

    except:

        # print(traceback.format_exc())
        return False,None

你可能感兴趣的:(【Django组件】UNIAPP+DJANGO 多图像上传 + 后端接口接受 uView组件(转载))