Exploring the capabilities of the PrimeNG File Upload Component
I have customized the upload process to connect with an API using a special uploadHandler
as shown below:
<p-fileUpload #fileUpload name="fileUpload"
(uploadHandler)="uploadHandler($event)" customUpload="true" fileLimit="5"
multiple="multiple" accept="image/*" maxFileSize="1000000">
</p-fileUpload>
In my component.ts
, I call upon the following functionality:
uploadedFiles: any[] = [];
@ViewChild('fileUpload') fileUpload: any;
constructor(private wpService: WordpressService, private cd: ChangeDetectorRef) {}
async uploadHandler(event) {
console.log(this.fileUpload)
this.fileUpload.disable = false;
this.fileUpload.progress = 5;
this.fileUpload.onProgress.emit({progress: this.fileUpload.progress});
this.cd.detectChanges();
this.uploadedFiles = [];
const perFilePercent = 95 / event.files.length;
this.fileUpload.disable = true;
for (const f of event.files) {
await this.wpService.uploadFile(f).then(response => {
const thisFile = f;
thisFile.id = response.id;
thisFile.url = response.source_url;
this.fileUpload.progress += perFilePercent;
this.fileUpload.onProgress.emit({progress: this.fileUpload.progress});
this.cd.detectChanges();
this.uploadedFiles.push(thisFile);
});
}
this.fileUpload.files = [];
this.cd.detectChanges();
}
Encountering an issue where manual updates to this.fileUpload.progress
and
this.fileUpload.onProgress.emit({progress: this.fileUpload.progress});
are not reflected in the file upload component itself.
Interestingly, binding a progress bar to this.fileUpload.progress
outside of the component works fine. For instance:
<p-progressBar [value]="fileUpload.progress" [showValue]="false"></p-progressBar>
Causes the expected behavior, while the progress bar within the actual file upload component remains static after the initial update.
A video demonstrating this behavior can be viewed at:
There seems to be an issue with updating the viewchild values or the event emitter, but the exact problem and solution remain elusive.