javascript - 如何在将图片上传到 Firebase 存储后检索下载 Url,以便将其保存到 Firebase 数据库?

标签 javascript firebase ionic3 firebase-storage

下面的代码成功地将图像上传到 Firebase 存储,但我想检索该确切图像的下载 URL 并将其存储在“this.pichaMtumiaji”中,然后将其保存在 Firebase 数据库中。我正在使用 ionic 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;
}

最佳答案

基于 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;
    });

  });

关于javascript - 如何在将图片上传到 Firebase 存储后检索下载 Url,以便将其保存到 Firebase 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54055404/

相关文章:

angular - 带有推送通知的 Firebase native 插件无法正常工作

javascript - 允许行中的列宽度不同

Firebase云函数部署错误

javascript - 对象中的特定方法不起作用

java - 查询 firebase 数据库并检索有限的数据

android - 在 FirebaseRecyclerViewAdapter 中过滤

html - 在ionic3上单击按钮时如何显示按钮

cordova - Ionic 3 Cordova 错误 : Device communication timed out. 尝试拔下并重新插入设备

javascript - 根据加载文件的内容阻止分配的成功处理程序

javascript - php 和 mysql 中的自动完成表单字段