My file upload functionality is working smoothly using ng2-file-upload. However, I am facing a challenge in handling server-side errors and resetting the file uploader. How can I achieve this? Below are the snippets of code that I am currently using:
HTML
<div class="progress progressbar" style="height:10px;">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': uploader.progress + '%' }"></div>
</div>
<button type="button" class="btn btn-warning btn-s button_gray" (click)="uploader.cancelAll()" [disabled]="!uploader.isUploading">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
<button type="button" class="btn btn-danger btn-s button_gray" (click)="uploader.clearQueue()" [disabled]="!uploader.queue.length">
<span class="glyphicon glyphicon-trash"></span> Remove
</button>
<button type="button" class="btn btn-primary btn-s" (click)="uploader.authToken=authToken ;uploader.uploadAll()" [disabled]="!uploader.getNotUploadedItems().length">
<span class="glyphicon glyphicon-upload"></span> Upload
</button>
Typescript
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => {
file.withCredentials = false;
if (this.target) this.target.value = '';
};
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);
}
let data = JSON.parse(response); //success server response
console.log(data)
this.showSnackBar('Successfully Uploaded file.');
this.router.navigate(['/result'])
}
onErrorItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any {
let error;
if (response != undefined && response.length > 0)
JSON.parse(response); //error server response
else
error = response;
console.log(error)
this.showSnackBar('Error on Upload the file.');
}
If there is a failure, I am encountering difficulty re-uploading the file.