I'm trying to send an object with an image from angular but I got the following error:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
When I set just the MultiprtFile as parameter and I send with angular just the file it works!! but I need to do that with the object but I keep getting the error but I don't know why
My entity class DTO is this:
@Data
public class PersonRequest {
private String id;
private Integer personId;
private String name;
private String job1;
private String job2;
private String skills;
// private Binary image;
private String country;
private String imageString;
}
this is my controller:
@PostMapping("multi")
public String multi(@RequestParam MultipartFile file, @RequestBody PersonRequest personRequest) throws IOException {
Binary files = new Binary(file.getBytes());
log.info("file: {}, personRequest: {}",file, personRequest);
My code in html is this:
<h5>Upload Image</h5>
<p-fileUpload mode="basic" #imageInput (change)="processFile(imageInput)"
[showUploadButton]="false" [showCancelButton]="false" accept="image/*"
chooseLabel="Choose File"></p-fileUpload>
in my ts:
processFile(imageInput: any) {
this.file=imageInput.files[0];
}
and this in my service:
addPersonWithIMG(image:File,personRequest: PersonRequest):Observable<any>{
const HttpUploadOptions = {
//commented this because it won't work
// headers: new HttpHeaders({ "Content-Type": "multipart/form-data" })
}
const file: FormData = new FormData();
file.append('file', image);
return this.http.post<PersonRequest>(`${this.baseUrl+"multi"}`,{file,personRequest});
}
In my entity I will save the image as Binary object, when I send just the file from angular it works but when I trying to add the object in the request I got that exception.
Please can anybody help me