【问题标题】:Angular with angularfire2 - getDownloadURL() won't work sometimes. Is this because of async带有 angularfire2 的 Angular - getDownloadURL() 有时不起作用。这是因为异步吗
【发布时间】:2020-06-16 08:18:13
【问题描述】:

获取https://firebasestorage.googleapis.com/xxxxx/o/hw04.docx404 firebase 下载链接错误 t {code_: "storage/object-not-found", message_: "Firebase Storage: Object 'hw04.docx' does not exist.", serverResponse_: "{↵ "error": {↵ "code" : 404,↵ "message": "No...not get object",↵ "status": "GET_OBJECT"↵ }↵}", name_: "FirebaseError"}

这是我的代码 sn-p。

upload(event) {
    const file = event.target.files[0];
    this.randomId = file.name;
    this.ref = this.afStorage.ref(this.randomId);
      this.task = this.ref.put(file);
      this.uploadProgress = this.task.percentageChanges();
      console.log("before pipe",this.task);
      this.ref.getDownloadURL().subscribe((url) => {
          console.log("link", url)
          this.downloadURL = url;
        },
        err => {
            console.log("firebase download link err", err);
        })
    }
}

它在 60% 的情况下都能正常工作,否则会出现 404 下载链接错误

有没有更好的方法来处理这个异步响应?

请帮帮我!

谢谢,

【问题讨论】:

标签: angular firebase firebase-storage angularfire2


【解决方案1】:

考虑使用来自 rxjs 的 finalize 方法:

task.snapshotChanges().pipe(
    finalize(() => this.downloadURL = fileRef.getDownloadURL() )
 )
.subscribe()

上传完成后会设置下载地址。

【讨论】:

  • 是的,我尝试过类似的方法,它奏效了。感谢您的回复@beniamin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 2020-03-26
  • 2012-08-20
  • 2016-04-14
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多