如何等待所有文件成功上传,然后再通过AngularFireStorage继续

时间:2019-02-14 12:15:12

标签: javascript firebase firebase-storage angularfire

我想使用angularfirestorage将文件列表上传到Firebase存储,然后在成功上传所有内容后,将详细信息写入Firestore数据库。

我创建了一个用于管理上传的功能:

async postpictures(): Promise<boolean> {
    //Post the files 
    let filesToUpload: any = {};

    if(this.files.length > 0 ){
      for(let i = 0; i < this.files.length; i++)
      {
        if(this.files[i].status === 'finished')
        {
          continue;
        }

        console.log("Fies ", this.files);
        console.log(`files ${i}`, this.files[i]);

        this.files[i].status = "Upload-Inprogress";
        this.files[i].progressbarstatus = "active";

        this.files[i].uploadTask = this.storage.upload(this.files[i].newpath, this.files[i].file, this.files[i].metadata);

        const fileRef = this.storage.ref(this.files[i].newpath);

        this.files[i].percentage = this.files[i].uploadTask.percentageChanges();
        this.files[i].snapshot = this.files[i].uploadTask.snapshotChanges();

        this.files[i].snapshot.pipe(
          tap(async snap =>{
            if(snap.bytesTransferred === snap.totalBytes){
              this.files[i].progressbarstatus = "";

              this.files[i].filesize = snap.totalBytes;

            }
          }),
          finalize(async () => {
            this.files[i].url = await fileRef.getDownloadURL().toPromise();
            this.files[i].status = "finished";

          })
        ).subscribe();
      }
    }


    return true;
  }

我本质上希望能够执行类似if(await this.postpictures()){//写入Firestore}的操作。

但是,我不确定如何等待该功能完成上传。

0 个答案:

没有答案