在React Native中使用ImagePicker从图库上传图像..需要base64字符串图像

时间:2018-10-03 07:23:21

标签: reactjs image base64 native

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

    if (response.didCancel) {
        console.log('User cancelled photo picker');
    }
    else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
    }
    else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
    }
    else {
        //let source = { uri: response.uri };
        // You can also display the image using data:
        let source = { uri: 'data:image/jpeg;base64,' + response.data };
        this.setState({
            ImageSource: source,
            data: response.data,
        });

    }
});

bodyFormData.append('image', {
    uri: this.state.ImageSource, // your file path string
    name: 'image1.jpg',
    type: 'image/jpg'
});

axios({
    method: 'post',
    url: 'https://',
    data: bodyFormData,
    config: { headers: { 'Content-Type': 'multipart/form-data' } }
})

1 个答案:

答案 0 :(得分:0)

ImagePicker.launchImageLibrary(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 {
    const source = { uri: response.uri };

    this.setState({
      ImageSource:source,
    });
  }
});
渲染中的

          <Image style={styles.ImageContainer} source={this.state.ImageSource} />

        </View>