My code below is for a single file upload, but I am trying to send multiple files from Angular to Spring Boot all at once. Sending multiple formdata to an ArrayList in Spring Boot results in null
, and I can't @RequestParam
a single formdata in Spring Boot, since the amount of files sent from Angular is dynamic (defined by the user). Could anyone please help?
Spring Boot controller:
@PostMapping("/jobAdApplication")
public void jobAdApplication (@RequestParam("files") MultipartFile[] files,
@RequestParam("candidateName") String candidateName) {
(...)
}
Angular component:
selectedFiles!: FileList;
file!: file;
(...)
if (this.selectedFiles) {
let file: File | null = null;
const formData: FormData = new FormData();
for (let i = 0; i < this.selectedFiles.length; i++) {
file = this.selectedFiles.item(i);
if (file) {
formData.append(`file${i}`, file);
}
}
formData.append('candidateName', this.applyFormGroup.get('name')?.value);
this.fileService.uploadFile(formData).subscribe({
next: response => {
alert('Files sent successfully')
this.applyFormGroup.reset();
},
error: err => {
alert(`Error: ${err.message}`);
}
});
this.selectedFiles = undefined;
}
Angular service:
uploadFile(jobAdApplication: FormData): Observable<any> {
return this._http.post<any>(`${this.jobCandidateUrl}/jobAdApplication`, jobAdApplication);
}