繁体   English   中英

React-native 上传 Image 作为 FormData

[英]React-native upload Image as FormData

需要在 React-Native 中将图像作为 formData 发布

表格数据:

------WebKitFormBoundary4df7ukBk3N8GDzOl 内容配置:表单数据; 名称=“自拍图像”; filename="600px-Google_Drive_logo.png" 内容类型:图像/png

------WebKitFormBoundary4df7ukBk3N8GDzOl--

我需要使用 react-native-image-picker 重新创建上述内容,这是我的代码:

图像选择器代码:

ImagePicker.showImagePicker(options, (response) => {
        console.log('Response = ', response);

        if (response.didCancel) {
          console.log('User cancelled image picker');
        } else if (response.error) {
          console.log('ImagePicker Error: ', response.error);
        } else if (response.customButton) {
          console.log('User tapped custom button: ', response.customButton);
        } else {
            formData = new FormData();
            formData.append("selfieImage", {
                name: 'selfieImage', 
                filename: response.uri
            })
            console.log(formData)

        //   this.setState({
        //     photo: formData,
        //   });
        }
      });

Axios 邮政编码:

 await axios({
        method: 'post',
        url: `${'{url}/home-security/image/selfie/' + this.state.newId}`,
        data: formData,
        config: { headers: {
            'Content-Type': 'multipart/form-data',
        }}
        })
        .then(function (response) {
            //handle success
            console.log(response);
        })
        .catch(function (response) {
            //handle error
            console.log(response);
        });

我知道的方法是将图像转换为 base64 字符串并通过 axios 传递。 但是最好使用像AWS S3这样的云服务来上传图像并发送图像URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM