Submit the upload file via Ajax submission Form table Forms __ajax

Source: Internet
Author: User


The Ajax submit form allows you to serialize the form form and then submit the data to the background, such as:


$.ajax ({url: "http://localhost:8080/", type: "POST", Data: $ (' #postForm '). Serialize (), Su Ccess:function (data) {}, Error:function (data) {}}); But this way if you have a file type that you want to submit in your form, then you can't get the data back in the background.

Solution: Use FormData. It is best to use jquery here after the 2.0 version should not be supported. For example:

<form id= "Form" >
   <p class= "P5" ><span><i>*</i> Merchant certificate:</span>
      Type= ' text ' name= ' apiclienttype ' id= ' TextField ' class= ' w_txt ' > <input type= ' button ' class= ' w_btn ' value= '
      Upload certificate '/>
      <input type= "file" Name= "Filefield" class= "W_file" id= "" Filefield "" size= " document.getElementById (' TextField '). value=this. Value "/>
   </P>

</form>



FormData (document.getElementById ("form"))//form ID
$.ajax ({
   URL: '${ctx}/wmmanage/ Saveweixinconfig.do ',
   type: ' POST ',
   data:formdata,
   false,
   false,
    False,
   false,
   (Result) {
    
   }
});

Enclosed background data receive:

@RequestMapping ("/saveweixinconfig")
@ResponseBody public
resultmsg saveweixinconfig (@RequestParam ( Value = "Filefield", required = False) Multipartfile Filefield) throws IOException {return
    
    "";
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.