Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular file upload progress percentage [duplicate]

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!

like image 416
komron Avatar asked Oct 31 '17 11:10

komron


2 Answers

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 + '%');
    } 
});
like image 167
Ian Avatar answered Nov 04 '22 20:11

Ian


uploadDocument(file) {

    return this.httpClient.post(environment.uploadDocument, file, { reportProgress: true, observe: 'events' })
}
like image 8
Shantam Mittal Avatar answered Nov 04 '22 21:11

Shantam Mittal