React从uri下载图像并放入静态文件夹?

时间:2019-11-26 19:08:25

标签: javascript reactjs firebase react-native

我有一个代码,可以将图像上载到Firebase存储中,它的工作效果很好,但是我想在react native应用程序的本地文件夹中下载图像?有可能吗?

 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();
}

0 个答案:

没有答案