【问题标题】:Angularfire multiple uploadAngularfire多次上传
【发布时间】:2018-08-04 18:19:38
【问题描述】:

我想上传几张图片,我的代码如下。它返回下载链接,但仅用于一张图像。如何获取已上传图片的链接列表?

  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>

【问题讨论】:

    标签: angularfire2


    【解决方案1】:

    简单方法:上传前清除this.downloadURLs,然后在finalize步骤中添加url

    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)
          }
    

    【讨论】:

    • 第一个对我有用,但我怎样才能在视图上正确显示它?记住我是通过服务调用上传函数的。
    • 您的意思是显示下载网址?只需在模板中使用 downloadURLs 属性。它将显示在 UI 中
    • 是的,我的意思是在模板中。我做到了,但它返回 [object Object]
    • 抱歉,我阅读了文档。 getDownloadURL 返回Promise。我修复了代码。
    • 工作,谢谢。我还用 .subscribe 更新了 .then 以使其可编译和工作。
    猜你喜欢
    • 2010-12-12
    • 2020-12-05
    • 2017-02-24
    • 1970-01-01
    • 2017-02-27
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多