3

I'm trying to access an uploaded file from the spring boot controller, but I'm getting error

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of springframework.web.multipart.MultipartFile

FormDomain class

public class FormDomain {
     @JsonPropertyDescription
 private MultipartFile fileInput;

public MultipartFile getFileInput() {
    return fileInput;
}

public void setFileInput(MultipartFile fileInput) {
    this.fileInput = fileInput;
}

}

Form Controller Class

@CrossOrigin(origins="*")
@RestController
public class FormController {
      @PostMapping(path="/basicForm")
 public String postResponseController(
              @RequestBody FormDomain loginForm) throws IOException {
         return "file transfer completed successfully";
     }

Angular form

<form #basicForm = "ngForm" (ngSubmit) = "onClickSubmit(basicForm.value)" enctype="multipart/form-data" class="form-horizontal">
    <input type="file" id="file-input" name="fileInput" ngModel (change)="selectFile($event)">
     <button type="submit"> Submit</button>
 </form>

Select File method

 selectFile(event) {
    this.selectedFiles = event.target.files[0];
    console.log(this.selectedFiles);
    }

Angular method that submits form

   onClickSubmit(data) {
             data.fileInput=this.selectedFiles;
        this.http.post("http://localhost:8080/basicForm", data).subscribe( (ob)=>(console.log("subscribe method called")));

  }

How to solve this issue?

Arjun
  • 1,116
  • 3
  • 24
  • 44
  • This answer should help: [https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json/25183266#25183266](https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json/25183266#25183266) – subires Jan 21 '20 at 09:11

1 Answers1

0

That is Not possible with RequestBody annotation.

You can achive with using like below.

public String postResponseController(@RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
//do somthing
}

I hope this will helpful for you.

Ranjith Bokkala
  • 379
  • 1
  • 10