In my application that i am developing in Angular 4, user can upload multipart files into server. Files are large. I need to show the current progress of file upload process with it's percentage to user, how can i do it?
Thanks in advance!
This works in Angular 9 and 10 (note observe: 'events'
)
const headers = new HttpHeaders({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: token
}))
const formData = new FormData();
formData.append('file_param_name', file, file.name);
this.httpClient.post(url, formData, {
headers,
reportProgress: true,
observe: 'events'
}).subscribe(resp => {
if (resp.type === HttpEventType.Response) {
console.log('Upload complete');
}
if (resp.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * resp.loaded / resp.total);
console.log('Progress ' + percentDone + '%');
}
});
uploadDocument(file) {
return this.httpClient.post(environment.uploadDocument, file, { reportProgress: true, observe: 'events' })
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With