2

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

表单数据:

------WebKitFormBoundary4df7ukBk3N8GDzOl Content-Disposition: form-data; 名称=“自拍图像”;filename="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);
        });
4

1 回答 1

3

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

于 2018-10-24T05:15:15.910 回答