【问题标题】:Multiple Image Upload: Progress bar for each upload多张图片上传:每次上传的进度条
【发布时间】:2018-08-30 21:18:45
【问题描述】:

我的 Angular4 应用程序的每个图像上传都需要不同的进度条。 (使用 AngularFireStore 存储)

我的组件

percentageArray = [];

startUpload(event: FileList) {

 Array.from(event).forEach(file => {
   if (file.type.split('/')[0] != 'image') {
     alert('Dieses Dateiformat wird nicht unterstützt');
   }

   // Storage path
  const path = `uploads/${this.currentUserEmail}/${this.uniqueID}/${file.name}`;

  // Meta Data
  const customMetadata = {
    auctionID: this.uniqueID.toString()
  }

   // Main Task
  this.task = this.storage.upload(path, file, {customMetadata});

  // Progress Monitoring

  this.percentage = this.task.percentageChanges();
  this.percentage.subscribe(p => {
    this.percentageArray.push(p);
  })

  this.snapshot = this.task.snapshotChanges();

  // File Download Url
  this.downloadURL = this.task.downloadURL();

  this.imgArray.push(path);

 })
}

我的 HTML

<div *ngIf="percentageArray as item" class="w-100">
  <div *ngFor="let elem of item">
  <ngb-progressbar type="info" [value]="elem" [striped]="true" [max]="100" [showValue]="true"></ngb-progressbar>
  </div>
</div>

结果

对于每个状态,我都会获得一个新的进度条...如何强制每次上传将其合并为一个?

【问题讨论】:

    标签: angular typescript firebase firebase-storage angularfire2


    【解决方案1】:

    你需要使用当前文件的索引。

    startUpload(event: FileList) {  Array.from(event).forEach((file, index) => { // <-- separate index for each file
        if (file.type.split('/')[0] != 'image') {
          alert('Dieses Dateiformat wird nicht unterstützt');
        }
    
        // Storage path
        const path = `uploads/${this.currentUserEmail}/${this.uniqueID}/${file.name}`;
    
    // Meta Data
    const customMetadata = {
      auctionID: this.uniqueID.toString()
    }
    
    // Main Task
    this.task = this.storage.upload(path, file, {customMetadata});
    
    // Progress Monitoring
    
    this.percentage = this.task.percentageChanges();
    this.percentage.subscribe(p => {
      this.percentageArray[index] = p; // <--- just put new percentage to needed index
    })
    
    this.snapshot = this.task.snapshotChanges();
    
    // File Download Url
    this.downloadURL = this.task.downloadURL();
    
    this.imgArray.push(path);})}
    

    然后从html中删除一个循环

    <div *ngIf="percentageArray as item" class="w-100"> 
    <ngb-progressbar type="info" [value]="item" [striped]="true"
    [max]="100" showValue]="true"></ngb-progressbar></div>
    

    【讨论】:

      【解决方案2】:

      它不应该是一个数组(可能是一个有键和值的数组,但是这样更新就更复杂了)。

      例如

      percentageArray = {};
      

      this.percentage.subscribe(p => {
          // Update percentage for this file.
          this.percentageArray[file.name] = p;
      });
      

      不确定如何最好地迭代 Angular HTML 中的对象键/值,但您可能可以在 SO 的某个地方找到答案。

      【讨论】:

      • 谢谢。但是使用此解决方案时,我的循环中出现以下错误:“找不到类型为 'object' 的不同支持对象 '[object Object]'。NgFor 仅支持绑定到 Iterables,例如数组。”
      • @eleinad:这就是为什么我注意到您必须找到一种不同的方法来迭代对象属性值。如前所述,您还可以将数组与例如keyvalue 属性,然后你可以再次迭代它,但你需要找到正确的项目 key 进行更新。或者,您可以只使用文件的索引进行查找。
      • 正如我写的那样,我看到 Vayrex 提交了一个简单使用索引的答案,应该可以正常工作。
      • 非常感谢您的帮助 :) 上面的解决方案运行良好 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多