Uday A. Navapara Uday A. Navapara -4 years ago 141
Ajax Question

ajaxform not submit the value if attachment is not availbale

hi i am beginner in java and goes to create one web application. i am submitting one form to controller using ajaxform. there is few element and one of them is type=file.

my controller method is as following

@RequestMapping(value = { "/addReplies" }, method = {
org.springframework.web.bind.annotation.RequestMethod.GET } )
public Map<String,Object> addReplies(
@Valid @ModelAttribute("Replies") Replies replies,
BindingResult result, HttpServletRequest request,
HttpServletResponse response,Locale locale,Principal principal,
@RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[]) throws ServletException,
IOException {
//perform some opraton

this every thing is work properly if there is attachment available in data otherwise it is not go into this method.

if i am remove @RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[] this parameter from method then work well but by this way i cannot get attachment.

please try to understand my question and as soon as give me all possible solution.

This method is working well if i am not using ajax and submit form with regul

Answer Source

For uploading a file normally you need to use encType="multipart/form-data" in your form. If you want to use Ajax to upload a file, Along with Simple ajax call you need to use its methods


whey you are uploading the file, before Serialize the ajaxForm first check is it filepart is null or not, if its null then disable the element for longer availability and submit

beforeSerialize: function($form, options){
//before serializing the data have to disable element if the data is not avaliable
              return !this.value; }).attr("disabled", "disabled");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download