Angularfire多次上传

时间:2018-08-04 18:19:39

标签: angularfire2

我想上传一些图像,并且我有如下代码。它返回下载链接,但仅返回一张图像。如何获得上载图像的链接列表?

  constructor(private storage: AngularFireStorage, public afs: AngularFirestore, ) {
    this.files = this.afs.collection('files').valueChanges();
  }

  uploadFile(event) {
    // reset the array 
    this.uploads = [];
    const filelist = event.target.files;
    const allPercentage: Observable<number>[] = [];

    for (const file of filelist) {
      const filePath = `${file.name}`;
      const fileRef = this.storage.ref(filePath);
      const task = this.storage.upload(filePath, file);
      const _percentage$ = task.percentageChanges();
      allPercentage.push(_percentage$);

      // observe percentage changes
      this.uploadPercent = task.percentageChanges();

      // get notified when the download URL is available
      task.snapshotChanges().pipe(
        finalize(() => {
          this.downloadURL = fileRef.getDownloadURL();
        })
      ).subscribe();
      // this.downloadURLs.push(this.downloadURL);
    }
  }

uploadFile(files) {
    //console.log(this.uploadService.uploadFile(file));
    this.uploadService.uploadFile(files);
  }
<ion-item>
      <ion-input type="file" (change)="uploadFile($event)" multiple="multiple"></ion-input>
    </ion-item>
    
    <button (click)="onAddItem()" ion-button block>Добавить</button>

1 个答案:

答案 0 :(得分:0)

简便的方法:在上传之前清除this.downloadURLs,然后在finalize步骤中添加网址

uploadFile(event) {
    // reset the array 
    this.uploads = [];
    this.downloadURLs = [];
    const filelist = event.target.files;
    const allPercentage: Observable<number>[] = [];

    for (const file of filelist) {
      const filePath = `${file.name}`;
      const fileRef = this.storage.ref(filePath);
      const task = this.storage.upload(filePath, file);
      const _percentage$ = task.percentageChanges();
      allPercentage.push(_percentage$);

      // observe percentage changes
      this.uploadPercent = task.percentageChanges();

      // get notified when the download URL is available
      task.snapshotChanges().pipe(
        finalize(() => {
          fileRef.getDownloadURL().subscribe((url) => { 
            this.downloadURLs = this.downloadURLs.concat([url]);
          });
        })
      ).subscribe();
      // this.downloadURLs.push(this.downloadURL);
    }
  }

Rxjs方式:首先合并所有最新结果,然后订阅以分配结果。注意:您也可以使用forkJoin

   import { combineLatest, from } from 'rxjs';
   import { map, filter } from 'rxjs/operators';

   ...

      uploadFile(event) {
        // reset the array 
        this.uploads = [];
        const filelist = event.target.files;
        const allPercentage: Observable<number>[] = [];
        const downloadUrls$ = filelist.map((file) => {

          const filePath = `${file.name}`;
          const fileRef = this.storage.ref(filePath);
          const task = this.storage.upload(filePath, file);
          const _percentage$ = task.percentageChanges();
          allPercentage.push(_percentage$);

          // observe percentage changes
          this.uploadPercent = task.percentageChanges();

          // get notified when the download URL is available
          return task.snapshotChanges().pipe(
            filter((task) => task.state === this.storage.TaskState.SUCCESS)
            switchMap(() => from(fileRef.getDownloadURL()))
          )
        });

        combineLatest(...downloadUrls$)
          .subscribe((urls) => this.downloadURLs = urls)
      }
相关问题