【问题标题】:Upload multiple files to Firebase Storage and when all uploads are finished, subscribe to forkJoin将多个文件上传到 Firebase 存储,当所有上传完成后,订阅 forkJoin
【发布时间】:2020-09-19 00:19:56
【问题描述】:

我正在处理需要上传所有文件的上传表单,当所有上传完成后,我想订阅它们并触发下一步操作。

我当前不起作用的实现:

 private uploadImages(): Observable<Subscription> {
const uploadImages$: Subscription [] = [];
this.testResponse.answers.forEach(answer => {
  const filePath = `${answer.selectedImage.name.split('.').slice(0, -1).join('.')}_${new Date().getTime()}`;
  uploadImages$.push(this.testService.uploadPhoto(filePath, answer.selectedImage).subscribe((url: string) => {
      answer.imageUrl = url;
      delete answer.selectedImage;
      delete answer.id;
    }));
});
return forkJoin(uploadImages$);}

  public uploadPhoto(filePath: string, selectedImage?: UploadMetadata): Observable<string | UploadTaskSnapshot> {
return this.storage.upload(filePath, selectedImage).snapshotChanges().pipe(finalize(async () => this.getDownloadUrl(filePath))); }



  public getDownloadUrl(filePath: string): Observable<string> {
return this.storage.ref(filePath).getDownloadURL(); }

1.如何在管道中提供downloadUrl之前“我等待”(finalize(async()...)?

2.你有更好的使用 Angular 将文件上传到 Firebase 的实现吗?

【问题讨论】:

    标签: javascript angular firebase file-upload firebase-storage


    【解决方案1】:

    希望这是您想要实现的目标:

    forkJoin(uploadImages$).pipe(
      map((photos) => {
        return photos.map(p => p.url);
      }),
      switchMap((photoUrls) => {
        return forkJoin(photoUrls.map(photoUrl => this.getDownloadUrl(photoUrl)));
      })
    )
    .subscribe((downloadUrls) => {
      console.log(downloadUrls);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2018-12-21
      • 2019-08-18
      • 2018-12-24
      • 2021-11-05
      • 2018-10-17
      • 2021-04-27
      • 2019-11-06
      • 2020-04-08
      相关资源
      最近更新 更多