React native Cloudinary Photo Upload TypeError: Network request failed

Viewed 26

I'm trying to pick a photo from my gallery and upload it to the cloudinary account. The image is chosen from the gallery and when I submit it, an error comes saying "TypeError: Network request failed". The code is as follows.

const choosePhotoFromLibrary = () => {
  ImagePicker.openPicker({
    width: 300,
    height: 300,
    cropping: true,
    compressImageQuality: 0.7
  }).then(image => {
    handleUpload(image);
    bs.current.snapTo(1);
  });
}

const handleUpload = (image) => {
  const data = new FormData()
  data.append('file', image)
  data.append('upload_preset', 'tourxApp')
  data.append('cloud_name', 'xxx')

  fetch("https://api.cloudinary.com/v1_1/xxx/image/upload", {

    method: 'POST',
    body: data

  }).then(res => res.json()).
  then(data=>{

    console.log(data);
  })
  .catch(e=>{
        
    console.log(`Uploading error ${e}`)
}) 
}

What is wrong here and how can I fix this?

1 Answers

So I found out what I was doing wrong in my code. I was not sending some important variables which were needed when passing the details. So I added the following code snippet and now it works perfectly.

    const uri = image.path;
    const type = image.mime;
    const name = image.path.split(".")[1];
    const source = {uri,type,name}
    handleUpload(source);
Related