Uday A. Navapara Uday A. Navapara - 26 days ago 9
Ajax Question

ajaxform not submit the value if attachment is not available

I am a beginner in Java and go to create one web application. I am submitting one form to controller using ajaxform. There are few elements and one of them is

type=file
.

my controller method is as following

@RequestMapping(value = { "/addReplies" }, method = {
org.springframework.web.bind.annotation.RequestMethod.POST,
org.springframework.web.bind.annotation.RequestMethod.GET } )
@ResponseBody
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 everything is work properly if there is attachment available in data otherwise it does not go into this method.

if I remove
@RequestParam(value = "fileData2",required=false) CommonsMultipartFile fileData2[]
this parameter from method then work well but by this way I cannot get the 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

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

or

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
         $(".attachfile").filter(function(){ 
              return !this.value; }).attr("disabled", "disabled");
         },