コード例 #1
0
    def post(self, request, *args, **kwargs):
        """
        上传文件
        ---
        parameters:
            - name: file
              description: 文件上传
              required: true
              type: file
              paramType: form

        """
        if not request.FILES or not request.FILES.get('file'):
            return Response(general_message(400, "param error", "请指定需要上传的文件"),
                            status=400)
        upload_file = request.FILES.get('file')
        if upload_file.size > 1048576 * 2:
            return Response(general_message(400, "file is too large",
                                            "图片大小不能超过2M"),
                            status=400)

        suffix = upload_file.name.split('.')[-1]
        file_url = upload_service.upload_file(upload_file, suffix)
        if not file_url:
            result = general_message(400, "upload file error", "上传失败")
        else:
            result = general_message(200,
                                     "file upload success",
                                     "上传成功",
                                     bean={"file_url": file_url})
        return Response(result, status=result["code"])
コード例 #2
0
ファイル: file_upload.py プロジェクト: ysice/rainbond-ui
    def post(self, request, *args, **kwargs):
        """
        上传文件
        ---
        parameters:
            - name: file
              description: 文件上传
              required: true
              type: file
              paramType: form

        """
        try:
            if not request.FILES or not request.FILES.get('file'):
                return Response(
                    general_message(400, "param error", "请指定需要上传的文件"))
            upload_file = request.FILES.get('file')
            suffix = upload_file.name.split('.')[-1]
            file_url = upload_service.upload_file(upload_file, suffix)
            if not file_url:
                result = general_message(400, "upload file error", "上传失败")
            else:
                result = general_message(200,
                                         "file upload success",
                                         "上传成功",
                                         bean={"file_url": file_url})
        except Exception as e:
            logger.exception(e)
            result = error_message(e.message)
        return Response(result, status=result["code"])
コード例 #3
0
    def post(self, request):
        req = UploadFileReqSerializer(data=request.FILES)
        req.is_valid(raise_exception=True)

        if not request.FILES or not request.FILES.get('file'):
            raise serializers.ValidationError("请指定需要上传的文件", status.HTTP_400_BAD_REQUEST)
        upload_file = request.FILES.get('file')
        if upload_file.size > 1048576 * 2:
            raise serializers.ValidationError("图片大小不能超过2M", status.HTTP_400_BAD_REQUEST)

        suffix = upload_file.name.split('.')[-1]
        file_url = upload_service.upload_file(upload_file, suffix)
        if not file_url:
            raise serializers.ValidationError("上传失败", status.HTTP_400_BAD_REQUEST)

        serializer = UploadFileRespSerializer(data={"file_url": file_url})
        serializer.is_valid(raise_exception=True)

        return Response(serializer.data)