React-native 上传图片为 FormData

React-native upload Image as FormData

需要 Post 图像作为 React-Native 中的 formData

表单数据:

------WebKitFormBoundary4df7ukBk3N8GDzOl Content-Disposition:表单数据;姓名="selfieImage";文件名="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 Post代码:

 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.