将多个文件上传到Firebase存储,并且所有上传完成后,订阅forkJoin

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

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

我目前无法执行的实现:

 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个回答
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);
});
© www.soinside.com 2019 - 2024. All rights reserved.