【问题标题】:React-native upload Image as FormDataReact-native 上传图片作为 FormData
【发布时间】:2018-10-24 04:16:51
【问题描述】:

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

表单数据:

------WebKitFormBoundary4df7ukBk3N8GDzOl 内容处置:表单数据;名称=“自拍图像”;文件名="600px-Google_Drive_logo.png" 内容类型:image/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);
        });

【问题讨论】:

    标签: javascript react-native react-native-android react-native-image-picker


    【解决方案1】:

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

    【讨论】:

    • 我不能,因为后端只接受 formData 类型的图像。
    • 那么最好的办法是使用像 s3 这样的云服务或者试试这个链接snowball.digital/Blog/Uploading-Images-in-React-Native
    • 非常感谢,我终于可以用你给我的链接了。将代码更改为安静一点,并且可以完美运行。谢谢
    猜你喜欢
    • 2017-04-07
    • 1970-01-01
    • 2018-06-08
    • 2017-06-29
    • 2018-11-24
    • 1970-01-01
    • 2023-03-31
    • 2021-10-17
    • 2020-04-01
    相关资源
    最近更新 更多