0

I am trying to submit a form with jQuery form plugin (http://malsup.com/jquery/form/)

my js looks like this:

var options = { 
        success: callback, 
        type: "POST",
        dataType: 'multipart/form-data',
        url: url   
};
$('#myForm').ajaxForm(options);
$('#myForm').submit();

my controller method looks like this:

public ModelAndView processForm(MultipartHttpServletRequest request) throws Exception 

I am getting this error:

(org.springframework.web.multipart.MultipartHttpServletRequest) throws
 java.lang.Exception]; nested exception is java.lang.IllegalStateException: Current 
request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]:

What am I doing wrong?

5
  • do you have a MultipartResolver configured in the spring configuration Commented Feb 12, 2014 at 5:04
  • @Arun Yes. I have done this before with jquery.fileupload, but now I need to use something else. Commented Feb 12, 2014 at 5:10
  • In-fact, I have this in my applicationContext. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000000" /> </bean> Commented Feb 12, 2014 at 5:11
  • which is the browser used? can you check it using IE < 10 and see whether it is working Commented Feb 12, 2014 at 5:12
  • I tried Chrome, FF, IE 11. Same error. I do not have IE<10 Commented Feb 12, 2014 at 5:38

2 Answers 2

1

You can change your controller to handle both multipart and non-multipart

@RequestMapping(value="/urRequestMappingURL", method = RequestMethod.POST) 
public @ResponseBody Map<String, Object> controllingMethod(HttpServletRequest request) {
    if (request instanceof MultipartHttpServletRequest) {
        // process the uploaded file
    }
    else {
        // other logic
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It was a combination of things: First, I need to add

enctype="multipart/form-data"

to the form tag.

also because the form tag spanned multiple divs, the jQuery form plugin did not pick up other inputs. So i did this:

<form id="myForm" enctype="multipart/form-data">
  <input type="file" name="file"/>
</form>

and then my js would use regular jQuery to add the other input fields before submiting like this:

    var options = { 
        success: callback, 
        type: "POST",
        dataType: 'JSON',
        url: url,
        beforeSerialize: function(form, options) {
            options.data = {
                caseNo: $("#caseNo").val(),
                inspectionID: $("#inspectionID").val()
            };
        },
};
$('#myForm').ajaxForm(options);
$('#myForm').submit();

Hope this helps someone

Comments

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.