Submit the upload file by submitting the form form through Ajax


 The Ajax form submission method can serialize the form form and submit the data to the background through data, for example:



  1. $.ajax({  

  2.      url : "http://localhost:8080/",  

  3.      type : "POST",  

  4.      data : $( '#postForm').serialize(),  

  5.      success : function(data) {  

  6.          

  7.      },  

  8.      error : function(data) {  
  9.         

  10.      }  

  11. });
However, in this way, if there are file types that need to be submitted in the form, then the data cannot be received in the background.

Workaround: Use  FormData. It is best to use jquery here and it should not be supported before version 2.0. E.g:

<form id="form">   <P class="p5"><span><i>*</i>商户证书:</span>      <input type='text' name='apiclientType' id='textfield' class='w_txt' >      <input type='button' class='w_btn' value='上传证书' />      <input type="file" name="fileField" class="w_file"id="fileField" size="28" onchange="



document.getElementById('textfield').value=this.value" />
</P>
</form>



var formData = new FormData(document.getElementById("form"));//表单id$.ajax({
url: '${ctx}/wmManage/saveWeixinConfig.do' ,
type: 'POST',data: formData,async: false,cache: false,contentType: false,processData: false,success: function





(result) {

}
})
;

附上后台数据接收:

@RequestMapping("/saveWeixinConfig")@ResponseBody
public ResultMsg saveWeixinConfig(@RequestParam(value = "fileField",required = false)MultipartFile fileField) throws IOException {

return "";
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854314&siteId=291194637