首页 > 解决方案 > base64 图像未正确存储在 firebase 的云存储(又名 firebase 存储)中

问题描述

我正在使用 firebase Web 界面将 base64 图像上传到 firebase(又名 firebase 存储)的云存储。

我用于上传的代码(在 React Native 中)如下:

const ref = await firebase.storage().ref(user).child(curTime.toString());
await ref.putString(base64String, 'base64', { contentType: 'image/jpeg' })

但是,当我尝试查看图像时,它无法显示。

  1. 上传的字符串在我的 Dropbox 中可用,这里(下载查看)。
  2. firebase 云存储中的文件可在此处获得。
  3. 我已将此文件下载到我的保管箱中,您可以在此处访问它(下载以查看它)。

以下是相关代码:

const uploadImageAsync = async(userId, curTime, photoUri) => {
  const user = normalizeUserId(userId); // prepare for firebase write
  let firebaseImageUrl = null;
  let base64String;
  try {
    base64String = await FileSystem.readAsStringAsync(photoUri,
      { encoding: FileSystem.EncodingTypes.Base64 });
  } catch (error) {
    console.log(error);
    return null;
  }

  const ref = await firebase.storage().ref(user).child(curTime.toString());

  await ref.putString(base64String, 'base64', { contentType: 'image/jpeg' })
  .then(async(snapshot) => {
    await snapshot.ref.getDownloadURL().then(uploadURL => {
      firebaseImageUrl = uploadURL;
    }, (error) => { // failed to get uploadedURL location
      console.log(error);
      return null;
    });
  }, (error) => { // failed to upload image to firebase storage);
    console.log(error);
    return null;
  });

  return firebaseImageUrl;
};

标签: firebasereact-nativefirebase-storage

解决方案


推荐阅读