I need to post a PDF file and a FormGroup in one request. I tried passing the PDF as FormData and the FormGroup just as it is, and also to add both to the FormData. I can't figure out how to pass both or what Annotations I need to add in my REST Request.
In my ts:
onSubmit() {
const formData = new FormData();
formData.append('pdfFile', this.myfile);
this.form.removeControl('pdfFile');
formData.append('invoice', this.form.value)
this.http.post("http://localhost:8080/zugferd/matform", formData)
.subscribe(res => {
console.log(res);
});
}
In my Spring Controller:
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping(value = "/matform")
public void both(@RequestPart("pdfFile") MultipartFile pdfFile, @RequestPart("invoice") MyInvoice invoice ,HttpServletResponse response) throws IOException {
System.out.println(invoice.getSender().getStreet());
System.out.println(pdfFile);
}
Edit: found solution:
onSubmit() {
const formData = new FormData();
formData.append('pdfFile', this.myfile);
this.form.removeControl('pdfFile');
const json = JSON.stringify(this.form.value);
const blob = new Blob([json], {type: 'application/json'})
formData.append('form', blob)
this.http.post("http://localhost:8080/zugferd/matform", formData)
.subscribe(res => {
//stuff
});
}