0

I have a code who upload a an image to firebase storage , its working great but i want to download the image locally in a folder in react native app ? is it possible ?

 async function uploadImageAsync(uri) {


  const UID = Firebase.auth().currentUser.uid;
  // Why are we using XMLHttpRequest? See:
  // https://github.com/expo/expo/issues/2402#issuecomment-443726662
  const blob = await new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function () {
      resolve(xhr.response);
    };
    xhr.onerror = function (e) {
      console.log(e);
      reject(new TypeError('Network request failed'));
    };
    xhr.responseType = 'blob';
    xhr.open('GET', uri, true);
    xhr.send(null);
  });

  const ref = Firebase
    .storage()
    .ref('users/' + UID)
    .child('profile');
  const snapshot = await ref.put(blob);

  // We're done with the blob, close and release it
  blob.close();

  return await snapshot.ref.getDownloadURL();
}
Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
manyouuwx
  • 47
  • 6
  • can you clarify what you mean by "download the image locally in a folder in react native app". Do you just want to use the image in your app? Do you want to store it locally? Do you want to let your users download it? – Josh Pittman Nov 26 '19 at 19:51
  • Downloading an image can only be done through the browser's download dialog box. You can't just force an image to appear in the user's local filesystem somewhere. You could theoretically use local storage, but that is for strings -- not images. You would need to base64 encode it or something. – FlippingBinary Nov 26 '19 at 19:54
  • @FlippingBinary The question is about React Native (which Josh's edit made clearer), where accessing local storage **is** possible. – Frank van Puffelen Nov 26 '19 at 23:07

0 Answers0