0

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

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Robs
  • 276
  • 1
  • 13
  • your controller is expecting a MultipartFile, but it seems you are sending a single normal fileµ – Stultuske Jun 24 '22 at 09:48
  • @Stultuske when in my controller I set as parameter just MultipartFile, it works! the problem is when I add an object, anyway I woud like to add in the object field but it won't work anyway, do you have any idea? – Robs Jun 24 '22 at 09:57
  • 1
    This may help: https://stackoverflow.com/a/33921749/4355748 – Rana_S Jun 24 '22 at 10:54
  • @Rana_S I got this error when I add to the append method the object personReqeust: TS2345: Argument of type 'PersonRequest' is not assignable to parameter of type 'string | Blob' Maybe there is any other way to add the file in the field of the object? I tried to but I still get exceptions – Robs Jun 24 '22 at 13:15
  • Basically, you use `FormData` to send file as well as other params to the server. In your controller, use the `MultipartHttpServletRequest` to access the file and params. – Rana_S Jun 24 '22 at 13:33

0 Answers0