如何获取图像文件?在python中?Django休息区

2024-09-27 23:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

class DemoAPIView(APIView):
    parser_classes = (JSONParser, FormParser, MultiPartParser)

    def post(self, request, format=None):
        data = request.data
        attached_file = request.FILES['attached_file']
        description_image = data.get('descriptionImage', None)
        attachment = dict(object_id=174, project=1, attached_file=attached_file,
                          description=description_image)
        header = {'Authorization': 'Bearer eyJ1c2VyX2F1dGhlbnRpY2F0aW9uX2lkIjoxNn0:1gSTf6:KbOb0yhqC-qVPTEPRoiVBBZjN6M'}
        r = requests.post('demo/api/v1/issues/attachments', data=attachment, headers=header)
        print(r.json())
        return Response({'error': 'ERROR XD'}, status=status.HTTP_400_BAD_REQUEST)

api引导我的错误如下:

^{pr2}$

这是我从客户那里发来的图像。 把附件打印出来,给我看看网址,显然这就是问题所在。 有没有办法得到完整的文件?在

表单客户端:

const bodyFormData = new FormData();
            if (typeof this.image !== 'string') {
              bodyFormData.append('attached_file', this.image);
              bodyFormData.append('descriptionImage', this.descriptionImage);
            }
            this.axios.post('/api/', bodyFormData,
              { headers: { 'Content-Type': 'multipart/form-data' } })
              .then((response) => {
                this.isSending = false;
                this.$snackbar.open(response.data.results);
                this.feedback = {};
                this.image = {};
              }).catch((err) => {
                this.$snackbar.open({ message: err.response.data.error, type: 'is-danger' });
                this.isSending = false;
              });

Tags: imagenoneapidataattachmentresponserequestdescription
1条回答
网友
1楼 · 发布于 2024-09-27 23:15:47

提交的表单可能不是enctype="multipart/form-data。在

Django docs所述:

Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

对于DRF,您可能需要FileUploadParser(请参见this

相关问题 更多 >

    热门问题