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

首先,我创建一个blob文件

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

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

    .then((data) => {FBStorageRef.put(data,{ contentType: mime })})

...但是它给了我错误

Firebase Storage: Object 'profilePics/image.png' does not exist

我相信我的FBStorageRef可以和它一起吃.....我的云存储完全空了,我想创建一个名为profilePics的文件夹。想打电话给image.png

const FBStorageRef = Firebase.storage().ref('/profilePics/image.png');

它说profilePics/image.png doe not exist足够真实.....但这就是我要正确上传它的原因:)

wyb001 回答:React Native-将图像上传到Firebase存储

  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,fileMetaData = null) {
    const uploadTask = firebase.storage().ref().child(storagePath).put(fileURI,fileMetaData);
    return uploadTask;
  }
  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(() => {});
    },() => {},);
,

使用RNFirebase处理Firebase存储上载和下载。检查以下链接以获取有关安装的更多详细信息。 https://github.com/invertase/react-native-firebase/tree/master/packages/storage

然后使用以下代码上传文件

 import firebase from 'react-native-firebase';

  export const uploadMediaUri = (uri,path) => {

    return new Promise((resolve,reject) => {
        firebase.storage()
            .ref(path)
            .putFile(uri)
            .then(() => {
                resolve()
            })
            .catch(e => {
                reject(e)
            });
    })
}

uri是您要上传哪个文件的本地路径 并且path是Firebase存储的上传路径

本文链接:https://www.f2er.com/3130663.html

大家都在问