I am currently working on implementing file uploads in Angular and am looking to display the upload progress as well.
upload.service.ts
public uploadProductImage(obj: FormData, id: string) {
return this._http.post(baseUrl + `/product/upload`, obj, {
headers: {
product_id: id,
},
reportProgress : true,
observe : 'events'
});
}
upload.component.ts
uploadClick() {
const fd = new FormData();
// for(const f of this.file_url) {
// fd.append('image', f.file, f.file.name);
// }
fd.append('image', this.file_url[0].file, this.file_url[0].file.name);
this.service.uploadProductImage(fd, this.product_id.toString())
.subscribe(
event => {
if (event.type === HttpEventType.UploadProgress) {
console.log(event.loaded, event.total);
this.progress = Math.round(event.loaded / event.total * 100);
} else if (event.type === HttpEventType.Response) {
console.log(event.body);
this.file_url = [];
}
},
err => {
console.log(err);
}
);
}
The image uploading functionality is now up and running smoothly. However, the progress bar feature seems to be experiencing some glitches. I am receiving one event with HttpEventType.UploadProgress
immediately, with both event.loaded
and event.total
values being equal.
As a result, the progress bar jumps to 100
immediately, even though there is a delay in completing the request.