My Angular 4 app has a simple file upload feature that works well. However, I'm facing an issue with the onSuccessItem callback not triggering every time a file is uploaded. The callback only triggers after the first upload, and I need it to trigger for each file upload without refreshing the page. I am using the ng2-file-upload plugin available at https://github.com/valor-software/ng2-file-upload
import { Component } from '@angular/core';
import {FileUploader, FileItem, ParsedResponseHeaders} from "ng2-file-upload";
@Component({
selector: 'upload-file',
template: `
<input type="file" ng2FileSelect [uploader]="uploader">
`,
})
export class UploadFileComponent {
uploader:FileUploader;
ngOnInit(): void {
this.uploader = new FileUploader({
url: 'http://url.to/upload',
headers: [{name:'Accept', value:'application/json'}],
autoUpload: true,
});
this.uploader.onErrorItem = (item, response, status, headers) => this.onErrorItem(item, response, status, headers);
this.uploader.onSuccessItem = (item, response, status, headers) => this.onSuccessItem(item, response, status, headers);
}
onSuccessItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
//this gets triggered only once when first file is uploaded
}
onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
let error = JSON.parse(response); //error server response
}
}