0

I want to change my local image into file type so I can send it on a api. How can I do it? How can it be done with react-native-fetch-blob Please help.

This is what I'm trying so far but it gives me a warning that it cannot find the path to the image. And nothing is shown via alert.

RNFetchBlob.fs.readFile('../../assets/imgs/profile.jpg', 'base64')
.then((data) => {

  // handle the data ..
  alert(data);

})
Shubham Bisht
  • 577
  • 2
  • 26
  • 51

1 Answers1

0

According to: https://github.com/joltup/rn-fetch-blob#user-content-upload-example--dropbox-files-upload-api

This example uses drop box.

RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
    // dropbox upload headers
    Authorization : "Bearer access-token...",
    'Dropbox-API-Arg': JSON.stringify({
      path : '/img-from-react-native.png',
      mode : 'add',
      autorename : true,
      mute : false
    }),
    'Content-Type' : 'application/octet-stream',
    // Change BASE64 encoded data to a file path with prefix `RNFetchBlob-file://`.
    // Or simply wrap the file path with RNFetchBlob.wrap().
  }, RNFetchBlob.wrap(PATH_TO_THE_FILE))
  .then((res) => {
    console.log(res.text())
  })
  .catch((err) => {
    // error handling ..
  })
JRK
  • 3,686
  • 1
  • 13
  • 19
  • but I don't want to do it using DropBox. Is there any other solution? Please help – Shubham Bisht Jan 10 '19 at 11:14
  • Then change the example to the url you want, if you don't have API Authorisation, then you can remove that also. The fetch request is just `{method, url, headers, data}` – JRK Jan 10 '19 at 11:16
  • if possible can you please give me a small demo where I convert a local image to blob file. I don't how to use this example. – Shubham Bisht Jan 10 '19 at 11:28
  • can you please see my updated code and tell me what could be the problem here. TIA :) – Shubham Bisht Jan 10 '19 at 11:45