Progress Bar in ng2-file-upload in Angular 6

家住魔仙堡 提交于 2020-01-05 07:18:24

问题


Id like to create a progress bar to my file upload.

The Upload I'm using is: https://www.npmjs.com/package/ng2-file-upload.

app.component.html

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <input type="file" name="myFile" ng2FileSelect [uploader]="uploader" />
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <button type="button" class="btn btn-success btn-s" (click)="uploader.uploadAll()" [disabled]="!uploader.getNotUploadedItems().length">
                Upload an Image
            </button>
        </div>
    </div>
</div>

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload';

const URL = 'url to API';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'myFile' });

  ngOnInit() {
    this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
    this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
      console.log('ImageUpload:uploaded:', item, status, response);
      alert('File uploaded successfully');
    };
 }
}

Everything is working fine but Id like to put a progress bar, only this.


回答1:


You can listen to progress here

this.uploader.onProgressItem = (progress: any) => {
    console.log(progress['progress']);
};



回答2:


In your app.component.html include :

<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': uploader.progress + '%' }"></div>
</div>


来源:https://stackoverflow.com/questions/51779303/progress-bar-in-ng2-file-upload-in-angular-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!