React Native-将图像上传到Firebase存储

jam*_*phy 6 firebase react-native firebase-storage

首先,我创建一个Blob文件

  RNFetchBlob.fs.readFile(localImgUrl2, 'base64')
    .then(base64ImageStr => Blob.build('image.png', base64ImageStr, { type: 'image/png;BASE64' }))
Run Code Online (Sandbox Code Playgroud)

......然后我尝试上传到Firebase

    .then((data) => {FBStorageRef.put(data, { contentType: mime })})
Run Code Online (Sandbox Code Playgroud)

...但这给了我错误

Firebase Storage: Object 'profilePics/image.png' does not exist
Run Code Online (Sandbox Code Playgroud)

我相信它FBStorageRef与.....有关系,我的云存储完全空了,我想创建一个名为profilePics ....的文件夹,其中的文件我想调用image.png

const FBStorageRef = Firebase.storage().ref('/profilePics/image.png');
Run Code Online (Sandbox Code Playgroud)

它说那profilePics/image.png doe not exist是真的.....但这就是为什么我想正确上传它的原因:)

tul*_*dev 3

  1. 如果文件路径看起来像file:///.... 您可以简单地将其放入 Firebase 函数中,不需要 readFile 步骤。

我的代码到目前为止工作正常:


  static uploadProfileAvatar(userID, fileURI) { // file URI is a path of a local image
    const updateTime = moment().unix();
    const storagePath = `${PROFILE_AVATAR_PATH}/${userID}_${updateTime}.jpg`;
    const fileMetaData = { contentType: 'image/jpeg' };
    return FirebaseStorage.uploadFile(fileURI, storagePath, fileMetaData);
  }

  static uploadFile(fileURI, storagePath, fileMetaData = null) {
    const uploadTask = firebase.storage().ref().child(storagePath).put(fileURI, fileMetaData);
    return uploadTask;
  }
Run Code Online (Sandbox Code Playgroud)
  1. 如果你有类似的路径rct-image-store://0。你必须写它然后获取本地路径图像。之后上传本地图片如情况1
  ImageStore.getBase64ForTag(
    rctFileURI, // rct-image-store: path
    (base64Image) => {
      const imagePath = `${RNFS.DocumentDirectoryPath}/${new Date().getTime()}.jpg`;
      RNFS.writeFile(imagePath, `${base64Image}`, 'base64')
      .then((success) => {
        // now your file path is imagePath (which is a real path)
        if (success) {
          // upload with imagePath, same as the 1
        }
      })
      .catch(() => {});
    },
    () => {},
  );
Run Code Online (Sandbox Code Playgroud)