1

I am uploading a file using ajax to spring mvc controller using the following code

var formData = new FormData();    
formData.append('file', file);

  $.ajax({
         url: url,
         data: formData,
         cache: false,
         contentType: false,
         processData: false,
         type: 'POST',
     }); 

I can see my data in the http post but i am getting null value in controller.

 @RequestMapping(value = { "/file.htm" }, method = RequestMethod.POST)

    public @ResponseBody String upload(@RequestParam(value="file",required=false) CommonsMultipartFile file 

            ) throws IOException {

        String response="";
}

I have tried with MultipartFile also. I have even tried to use MultipartHttpServletRequest and tried to obtain data from the request but cannot get it. Please let me know how to resolve it. Thanks

2 Answers 2

1

try use @RequestPart instead of @RequestParam

Sign up to request clarification or add additional context in comments.

1 Comment

I could still see the post data in the firebug console..but the value of the parameter "file" is still NULL in the controller..i am struggling with this for 2 days. please help..
1
var fd = new FormData();
fd.append( "file", $("input[name=file]").files[0]);

var ajaxReq =  $.ajax({
    url : 'kolfileUpload',
    type : 'POST',
    data : fd,
    cache : false,
    contentType : false,
    processData : false,

});

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.