【问题标题】:I can't get image downloadUrl from Firebase Storage (Angular/Ionic)我无法从 Firebase 存储(Angular/Ionic)获取图片 downloadUrl
【发布时间】:2018-11-09 17:57:14
【问题描述】:

我正在尝试从 firebase 获取图片的 downloadUrl,“timeCreated”、“fullPath”、“contentType”等所有属性都运行良好且推送正确!但我不知道为什么“downloadUrl”不起作用!!

captureAndUpload() {
    this.dataProvider.captureImage().then(data => {
     this.dataProvider.uploadImage(data).then(res => {
        this.dataProvider.storeImageInformation(res.downloadURL);
      });

    });
  }

数据提供者:

storeImageInformation(downloadURL)
 {
this.db.list(`/profiles/${this.afAuth.auth.currentUser.uid}`).
push(downloadURL); 
      }

有什么想法吗?!

【问题讨论】:

  • 你能发布DataProvider类吗?通常在上传成功后,您需要使用引用对更改进行快照以获取下载 url
  • DataProvider 类:pastebin.com/S5N3z4vm

标签: javascript angular firebase ionic-framework firebase-storage


【解决方案1】:

THX 道格,你是对的。

正确的代码是这样的:

captureAndUpload() {
this.data.captureImage().then(data => {
  let upload = this.data.uploadImage(data);

  upload.then().then(res => {
    this.data.storeImageInformation(res);
  }); 
}

数据提供者:

uploadImage(image) {

let storageRef: AngularFireStorageReference;

let newName = `${new Date().getTime()}-${this.afAuth.auth.currentUser.uid}.png`;

storageRef = this.afStorage.ref(`/images/${this.afAuth.auth.currentUser.uid}/${newName}`);

return storageRef.putString(image, 'base64', { contentType: 'image/png'})
          .snapshotChanges().toPromise().then(_ =>
             {
              return storageRef.getDownloadURL().toPromise().then(res => {
                console.log('URL: ', res);
                return res;
              });
            }
          ) 
        }

 storeImageInformation(downloadURL) {
  return this.db.object(`/images/${this.afAuth.auth.currentUser.uid}`).update({img: downloadURL}); }

【讨论】:

    【解决方案2】:

    现在无法在上传的即时结果中访问下载 URL。此更改是在几个月前对 Firebase 客户端 SDK 进行的。

    相反,您必须在上传完成后调用 getDownloadURL(或该 JavaScript 函数的任何 Angular 绑定)以获取 URL 作为第二个请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2019-04-08
      • 2019-12-14
      相关资源
      最近更新 更多