3

I'm trying to upload a file from UI and getting the below exception: error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present."

Javascript code:

var fd = new FormData();
      fd.append("file", config.uploadFile);

      $http.post(url, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
      })

Controller code:
@RequestMapping(value = "/rest/data/upload", method = RequestMethod.POST,
            consumes = {"multipart/*"}, headers = "content-type=multipart/form-data")
    public void uploadFile(@RequestPart("file") MultipartFile file) throws Exception {
        this.fileService.uploadFile(file);
    }

I added this to the AppConfig (after looking at similar issues in StackOverflow):

@Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver=new CommonsMultipartResolver();
//        resolver.setDefaultEncoding("utf-8");
        resolver.setMaxInMemorySize(1048576);
        resolver.setMaxUploadSize(20971520);
        return resolver;
    }

The issue still persists for me. Any insight/pointers/help would be appreciated.

Thanks, AI.

0 Answers0