将映像上传到Firebase存储后如何检索下载的URL,以便可以将其保存到Firebase数据库?

时间:2019-01-05 19:19:25

标签: javascript firebase ionic3 firebase-storage

下面的代码已成功将图像上传到Firebase存储,但是我想检索该确切图像的downloadURL并将其存储在“ 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;
}

1 个答案:

答案 0 :(得分: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;
    });

  });