Angularfire2的角度-getDownloadURL()有时不起作用。这是因为异步造成的

问题描述 投票:0回答:1

获取https://firebasestorage.googleapis.com/xxxxx/o/hw04.docx 404firebase下载链接err t {code_:“存储/未找到对象”,message _:“ Firebase存储:对象'hw04.docx'不存在。”,serverResponse _:“ {↵“错误”:{↵“代码” :404,↵“ message”:“否…未获取对象”,↵“ status”:“ GET_OBJECT”↵}↵}“,name_:” FirebaseError“}

这是我的相同代码段。

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个回答
0
投票

考虑使用rxjs中的finalize方法:

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

上传完成后,它将设置下载网址。

© www.soinside.com 2019 - 2024. All rights reserved.