0

I am trying to upload image to server with progress by using the example provided by:

https://gist.github.com/Tamal/9231005f0c62e1a3f23f60dc2f46ae35

I checked some tutorials, the code should works. But the uri in Android show uri

uri: content://media/external/images/media/4985

The URI come from the component

https://github.com/jeanpan/react-native-camera-roll-picker

The URI should be

file://....

So, why the upload code not working.

How can I convert the

content://... to file://.... to make it possible to upload image to server in React-native? or does my assumed is correct?

1 Answer 1

1

I am using react-native-image-picker to get image from library. I have written following code in one method name as selectPhoto() to select image from library.

selectedPhoto = () => {
//Open Image Picker

const options = {
  quality: 1.0,
  maxWidth: 500,
  maxHeight: 500,
};

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};
    console.log(source.uri);
    this.setState({
      profilePhoto: source
    });
  }
}); }

This will give me uri of selected image and I have set in state variable. then write following code to upload image.

var profiePicture = {
    uri: this.state.profilePhoto.uri,
    type: 'image/jpg', // or photo.type image/jpg
    name: 'testPhotoName',
  }

  // API to upload image
  fetch('http://www.example.com/api/uploadProfilePic/12345', {
    method: 'post',
    headers:{
      'Accept': 'application/json',
      'content-type': 'multipart/form-data',
    },
    body: JSON.stringify({
      'profile_pic' : profiePicture
    })
  }).then((response) => response.json())
  .then((responseJson) => {
    console.log(responseJson);
  })
  .catch((error) => {
    console.error(error);
  });

This code is working in one of the my project.

Sign up to request clarification or add additional context in comments.

2 Comments

I think even I use your mentioned component, it provided as uri: 'content://media/external/images/media/4985' as what I use.
I'm learning react native and today I had preparing demo to upload image. This is working fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.