getting UNPROCESSABLE ENTITY error uploading image using react to flask api swagger ui

问题描述 投票:0回答:0

我正在尝试将图像 reactjs 上传到 flask api swagger ui,但我得到了 UNPROCESSABLE ENTITY 我猜这背后的原因是 flask api 无法识别输入数据,我正在寻找一些建议修复这个

反应方面我正在使用 axios 和材料 ui

     const [selectedImage, setSelectedImageuplord] = useState(null);
     useEffect(() => {
        if (selectedImage) {
            console.log(selectedImage);

            const formData = new FormData();

            formData.append('file', selectedImage);
            formData.append('fileName', selectedImage.name);
            console.log(formData.get('file'));
            console.log(formData.get('fileName'));
            axios
                .post(
                    `http://127.0.0.1:5000/fileupload`,
                    { formData },
                    { headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'multipart/form-data' } }
                )
                .then((res) => {
                    console.log('res' + res);
                    console.log('res.data' + res.data);
                });
            //setItem(selectedImage);
            //setImageUrl(URL.createObjectURL(selectedImage));
        }
    }, [selectedImage]);
 return (
      <Grid item xs={6} sm={6}>
                    <input
                        accept="image/*"
                        type="file"
                        id="select-image"
                        style={{ display: 'none' }}
                        onChange={(e) => setSelectedImageuplord(e.target.files[0])}
                    />
                    <label htmlFor="select-image">
                        <AnimateButton>
                            <Button disableElevation size="large" variant="contained" color="secondary" component="span">
                                Upload Image
                            </Button>
                        </AnimateButton>
                    </label>
                </Grid>
                <Grid item xs={6} sm={6}>
                    {imageUrl && selectedImage && (
                        <Box mt={2}>
                            <div>Image Preview: {selectedImage.name} </div>
                            <img src={imageUrl} alt={selectedImage.name} height="400px" />
                        </Box>
                    )}
                </Grid>
   );
  

python 方面反应方面我正在使用棉花糖,flask_apispec

class fileUpload(MethodResource, Resource):
 
    @doc(description='Endpoint for uplord images.', tags=['fileUpload'])
    @use_kwargs(CRDRequestSchema ,location=('files'))
    @marshal_with(CRDResponseSchema)  # marshalling
    def post(self, **kwargs):
        try:
            file=request.files['file']
            filename = secure_filename(file.filename)
            destination="/".join(['../images',filename])
            file.save(destination)
            return {'message': 'Image was uplord to location'}

        except Exception as e: # work on python 3.x
            logger.error('Failed to upload to ftp: '+ str(e))
            print('Failed to upload to ftp: '+ str(e))
            return {'message': 'Failed to upload to ftp: '+ str(e)}

在 swagger ui 方面它正在工作,当尝试使用 react 上传图像时我得到UNPROCESSABLE ENTITY

python reactjs flask swagger-ui flask-restful
© www.soinside.com 2019 - 2024. All rights reserved.