0

I am trying to upload a file to my front end system and as a test save it locally to a folder in my C drive. I have both the ajax function and the controller method but cant seem to find out what the error message means in the spring logs.

Ajax function:

    function makeProgress(){                    
      var url = getRelativeURL("web/fileUpload");        
      var formData = new FormData();
      formData.append('file', $('input[type=file]')[0].files[0]);
      console.log("form data " + formData);
      $.ajax({
          url : url,
          data : formData,
          processData : false,
          contentType : "multipart/form-data",
          type : 'POST',
          success : function(data) {
              alert(data);
          },
          error : function(err) {
              alert(err);
          }
     });

        }

Server side controller:

private static String UPLOADED_FOLDER = "C://temp//";

@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST, consumes ={"multipart/form-data"})
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpServletResponse res)
{       
    try {

            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
            logger.info("You have successfully uploaded '" + file.getOriginalFilename() + "'");
            return("File Uploaded");


    } catch (Exception e) {
        res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        logger.error("Failed to upload file '" + file.getOriginalFilename() + "'", e);
        return("File Not Uploaded");
    }
}

Error logs:

2017-04-10 10:30:09.758 DEBUG o.s.web.servlet.DispatcherServlet.976 - Could not complete request 
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
    at org.springframework.util.Assert.notNull(Assert.java:112) ~[spring-core-3.2.8.RELEASE.jar:3.2.8.RELEASE]

Can anyone see what I am missing?

RA19
  • 583
  • 3
  • 17
  • Show your configuration, please. – alfcope Apr 10 '17 at 09:50
  • @Bean(name = "multipartResolver") public CommonsMultipartResolver commonsMultipartResolver(){ CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setDefaultEncoding("utf-8"); commonsMultipartResolver.setMaxUploadSize(50000000); return commonsMultipartResolver; } } – RA19 Apr 10 '17 at 10:06
  • 2017-04-10 11:07:56.021 DEBUG o.s.web.servlet.DispatcherServlet.976 - Could not complete request org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163) ~[spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] – RA19 Apr 10 '17 at 10:08
  • @alfcope did the above help for the configuration? – RA19 Apr 10 '17 at 11:31
  • Anyone else able to help on this? – RA19 Apr 10 '17 at 12:15
  • 1
    It seems your request is wrong. Set the `contentType` to false at your ajax request. Take a look to this answer http://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload – alfcope Apr 10 '17 at 13:53

0 Answers0