firebase - firebase存储中的下载网址为空

标签 firebase firebase-storage angularfire2 ionic4

我正在使用 ionic 4 将图像从相机上传到 firebase 存储。上传顺利,但我很难获取上传 URL。我的代码如下所示:

async getFromCamera(){
    this.camera.getPicture({
      destinationType: this.camera.DestinationType.DATA_URL,
      quality: 25,
      correctOrientation: true,
        allowEdit:false
  }).then(async (imageData) => {
      var base64Image = "data:image/jpeg;base64," + imageData;
      this.uploadToFireStore(base64Image).then(
            (data) => console.log("done to firestore:"  + data),
            (err) => console.log("The error to upload is:::" + JSON.stringify(err))
          )
  }, (err) => {
      console.log("Error found is:" + err);
  });
  }

  uploadPercent
  downloadURL

  uploadToFireStore(imageData){
   return new Promise<any>((resolve, reject) => {
    let storageRef = this.storage.ref('/');
    let imageRef = storageRef.child('myimage')
    const task = imageRef.putString(imageData, 'data_url')

      console.log("Task is:::" + task)
       // observe percentage changes
    this.uploadPercent = task.percentageChanges();
    // get notified when the download URL is available
    task.snapshotChanges().pipe(
        finalize(() => {
                            this.downloadURL = imageRef.getDownloadURL() 
                            console.log("upload percent:" + JSON.stringify(this.uploadPercent))
                            console.log("download url is::" + JSON.stringify(this.downloadURL))
                      } 
                )
     )
    .subscribe()
  })
  }

我看到的回复是:

upload percent:{"_isScalar":false,"source":{"_isScalar":false},"operator":{}}
/tab1-tab1-module.js:629 download url is::{"_isScalar":false}

最佳答案

Firebase Storage 允许您上传文件,因此您无需上传长 base64 字符串。为什么还要上传base64

我不熟悉 this.camera.getPicture 方法返回的内容,但很确定它是一种 File 类型。在这种情况下:

}).then(imageData => {
    this.uploadToFireStore(imageData)

还有

  uploadToFireStore(imageData){
    let storageRef = this.storage.ref('/');
    let imageRef = storageRef.child('myimage')
    const task = imageRef.upload(imageData, 'data_url')

    this.uploadPercent = task.percentageChanges();
    task.snapshotChanges().pipe(
        finalize( async () => {
          await this.downloadURL = imageRef.getDownloadURL().toPromise();
          console.log('this.downloadURL', this.downloadURL)

                      } 
                )
     )
    .subscribe()

请注意,finalize 采用一个 async 函数,因为 imageRef.getDownloadURL() 返回一个 Observable,因此我们只需要其中的一个值它是我们需要的该流的唯一值,将其转换为 Promise 看起来更干净。

关于firebase - firebase存储中的下载网址为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56093153/

相关文章:

firebase - Firebase存储配额已超出

android - 身份验证后读取数据时权限被拒绝 Firebase

javascript - 从 Firebase 存储加载图片到 React

javascript - RxJS 对多个流进行过滤

java - 如何为手机中安装的 Whatsapp、Facebook 或其他应用程序创建共享按钮

javascript - Angularfire:如何将一个对象插入 $firebaseArray 中的另一个对象?

android - 空引用上的 Firebase ArrayList.size()

node.js - Firebase + gcloud 存储 + Nodejs。访问被拒绝错误

javascript - 获取 firestore 文档的新 ID 以将其添加到文档本身

javascript - 如何过滤来自 Firestore 数据库的数据