I used this code to upload files in Angular 6
.
Everything works fine but I need to add the progress bar to the code. After searching from stackoverflow, I saw this code below but being new to angular
or angular6
I do not know how to integrate it or make the progress bar works with the angular
form submission.
this.uploader.onProgressItem = (progress: any) => {
console.log(progress['progress']);
};
this.uploader.onSuccessItem = (progress: any) => {
console.log('I receive the response file posted in API');
};
Progress Bar in ng2-file-upload in Angular 6
Below is the code
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
const URL = 'http://localhost:3000/api/upload';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'photo' });
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');
};
}
}
app.component.html:
<input type="file" name="photo" ng2FileSelect [uploader]="uploader" />
<button type="button" class="btn btn-success btn-s"
(click)="uploader.uploadAll()"
[disabled]="!uploader.getNotUploadedItems().length" >
Upload an Image
</button>