从 firebase 存储下载 url 为空

Posted

技术标签:

【中文标题】从 firebase 存储下载 url 为空【英文标题】:download url from firebase storage comes empty 【发布时间】:2019-09-29 06:44:48 【问题描述】:

我正在使用 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

【问题讨论】:

【参考方案1】:

Firebase 存储可让您上传文件,因此您无需上传很长的 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,所以我们只需要一个值,这是我们需要的唯一值,转换它看起来更简洁承诺。

【讨论】:

等待 this.downloadURL = imageRef.getDownloadURL().toPromise();我收到错误消息“;预期” 这是一个拼写错误。检查是否缺少括号等。您提供的 sn-p 非常混乱,因此很可能有一些拼写错误

以上是关于从 firebase 存储下载 url 为空的主要内容,如果未能解决你的问题,请参考以下文章

从多个文件上传 Firebase 存储中获取下载 url

从 Firebase 存储下载并播放 .mp3 文件到内存

如何从 Firebase 存储下载 URL 中删除查询字符串

从 Flutter 中的 Firebase 存储获取下载 URL

如何将 Url 从 Firebase Storage 存储到 ArrayList 中?

从firebase函数获取云存储中文件的永久可公开下载URL [重复]