Currently, I have a function in my Angular component that is executed whenever I need to upload a list of files.
I aim to monitor the upload progress to empty the file list and keep track of the upload progress for a progress bar display.
The issue I am facing is that when I access the value of uploadProgress
, it triggers
this.pictureService.upload(this.selectedFiles)
twice, resulting in the files being uploaded twice.
This is how my current function looks like:
uploadImages() {
// Upload picture and save progress as an observable
const uploadProgress: Observable<HttpEvent<HttpUploadProgressEvent>> = this.pictureService.upload(this.selectedFiles);
// Show snackbar with observable for progress bar
this.snackBar.openFromComponent(UploadProgressComponent, {
data: { uploadProgress },
});
// Wait for the upload to finish and then reset selected files and preview URLs
uploadProgress.subscribe((event: HttpUploadProgressEvent) => {
if (event.loaded === event.total && event.loaded !== undefined) {
this.selectedFiles = null;
this.previewUrls = [];
}
});
}