0

I'm using RnfechBlob for uploading images in blob format, it's properly working in Android and only works sometimes in ios, I'm getting a error as : cannot parse response in IOS.

return await RNFetchBlob.fetch(
  "POST",
  `${urlToUpload}`,
  {
    Authorization: "Basic ZGF2aXM6c2U3ZW5zZTdlbg==",
    "Content-Type": "multipart/form-data",
    otherHeader: "foo",
  },
  [
    {
      name: "filedata",
      filename: `image.png`,
      type: "image/png",
      data: RNFetchBlob.wrap(imgpath),
    },
  ]
)
  .uploadProgress({ interval: 5 }, (written, total) => {
    total = written / total;
    console.log("uploaded", total * 1);
  })
  .then((response) => response.json())
  .then(async (d) => {
    console.log("dddd", d);
    return d.url;
  })
  .catch((err) => {
    console.log("Error in adding a comment", err, err.message);
  });

1 Answers1

0

I had the same issue I found that url is coming late in response. so I did some modifications in code and that worked. Let me know if that works for you!.

const data = await RNFetchBlob.fetch(
  "POST",
  `${urlToUpload}`,
  {
    Authorization: "Basic ZGF2aXM6c2U3ZW5zZTdlbg==",
    "Content-Type": "multipart/form-data",
    otherHeader: "foo",
  },
  [
    {
      name: "filedata",
      filename: `image.png`,
      type: "image/png",
      data: RNFetchBlob.wrap(imgpath),
    },
  ]
)
  .uploadProgress({ interval: 5 }, (written, total) => {
    total = written / total;
    console.log("uploaded", total * 1);
  })

  if(data.url ){
    return data.url
  }  
  else{
    console.log("error",data)
    return "error"
  }
atrocks01
  • 41
  • 4
  • atrocks01, Thanks for the reply brother. I've followed same as your code, but it did not work.. the response is not reaching 'IF' block at all. – Rvind Jitta Aug 08 '22 at 07:01
  • can you try axios instead of RNFetchBlob, because RNFetchBlob first breaks media into bits and the transfer to server which creates some lag. if its s3 then axios will work directly. idk about other servers. let me know if you need sample code. – atrocks01 Aug 08 '22 at 09:50
  • It would be great, if you share sample code for passing blob data through axios. thank you : ) – Rvind Jitta Aug 08 '22 at 10:32
  • Actually , I've tried with axios, but in that I'm not able to upload blob data, it showing internal server error. – Rvind Jitta Aug 08 '22 at 10:44
  • [Form sample code to upload media](https://stackoverflow.com/questions/47809402/post-image-which-store-as-a-blob-with-axios-vuejs) try this, I will suggest use form to send media. the problem I can sense in your case is lag in response. that is because of RNfetchblob. use form to solve that. – atrocks01 Aug 09 '22 at 04:56
  • [link](https://gist.github.com/Arvindjitta/3fd44423db1661eacfc535922a9d699f ) above is the link for the axios code, I've tried with formdata but still not uploading image. – Rvind Jitta Aug 11 '22 at 08:34
  • there could be one possibility that its taking very long time to upload and in meanwhile file is getting deleted from cache, so you can save it in rom and as soon as it gets upload delete it from rom, [code for that](https://gist.github.com/atrocks01/629d7b29d9aed4b62c473482fdc80241) but before trying this check upload function and make changes accordingly maybe you don't need to work on ram/rom. – atrocks01 Aug 12 '22 at 12:11
  • Hey, I made small change in uploading blob , added actual data inside the blob ` fd.append("filedata", blob._data);` Then it uploades properly and I get response saying "uploaded succesfully" with the data.url, But it's uploading empty file. – Rvind Jitta Aug 13 '22 at 11:27
  • So can you try to upload mp3/mp4/jpg(any media) directly without converting it into blob. In that case my code should work. directly add file path. – atrocks01 Aug 15 '22 at 12:16