【问题标题】:How to retrieve the download Url after uploading image to Firebase storage, so that I can save it to Firebase database?将图像上传到 Firebase 存储后如何检索下载 URL,以便将其保存到 Firebase 数据库?
【发布时间】:2019-01-06 07:07:43
【问题描述】:

下面的代码成功地将图像上传到 Firebase 存储,但我想检索该确切图像的下载 URL 并将其存储在“this.pichaMtumiaji”中,然后将其保存在 Firebase 数据库中。我正在使用离子 3!

uploadPhoto(): void {
    this.myPhotosRef.child(this.generateUUID()).child('myPhoto.png')
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => {
        this.pichaMtumiaji = savedPicture.downloadURL().absoluteString;
      });
  }

generateUUID(): any {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}

【问题讨论】:

    标签: javascript firebase ionic3 firebase-storage


    【解决方案1】:

    基于Firebase documentation on uploading a file中的示例:

    var ref = this.myPhotosRef.child(this.generateUUID()).child('myPhoto.png');
    ref
      .putString(this.myPhoto, 'base64', { contentType: 'image/png' })
      .then((savedPicture) => {
    
        // Upload completed successfully, now we can get the download URL
        ref.getDownloadURL().then(function(downloadURL) {
          console.log('File available at', downloadURL);
          this.pichaMtumiaji = savedPicture.downloadURL().absoluteString;
        });
    
      });
    

    【讨论】:

    • 感谢您的反馈,但您的解决方案也没有解决问题。如前所述,即使在您的解决方案之后,图像也会保存到 Firebase 存储中,但下载 URL 仍然无法访问。
    • 请比“仍然无法实现”更具体。运行代码时会发生什么?它是否记录“文件可用于”?你是在调试器中运行的吗?您是否在ref.getDownloadURL()... 上设置了断点?它会被触发吗?您是否在this.pichaMtumiaji... 上设置了断点?您在那里获得的价值是什么?
    • 我在调试器中运行它,结果如下:vendor.js:2134 ERROR TypeError: Cannot set property 'pichaMtumiaji' of undefined at main.js:224 at e.g (vendor.js:140665) at Fb (vendor.js:140668) at Bb (vendor.js:140668) at A.g.Yb (vendor.js:140667) at kb (vendor.js:140661) at t.invoke (polyfills.js:3) at Object.onInvoke (vendor.js:5445) at t.invoke (polyfills.js:3) at r.run (polyfills.js:3)
    • 好的。所以this 似乎是未定义的。
    • 是的!我应该如何解决它..?我已经在开头声明了public pichaMtumiaji: any;,但它似乎不是解决方案。
    猜你喜欢
    • 2020-12-18
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2017-11-03
    相关资源
    最近更新 更多