FormData 上传文件

需要将选中的xml传到后台,通过xslt转换为html

html:

<div id="test">
  <form id="uploadForm"><!--这里我没有添加enctype="multipart/form-data",也可以使用,没有出问题-->
   <input type="file" name="testFile" value="选择文件">
   <input type="button" value="添加" id="mikasa">
  </form>
 </div>

js:

<script type="text/javascript">
  $("#mikasa").click(function(){
   $("#test div").remove();
    var formData = new FormData($( "#uploadForm" )[0]);  
        $.ajax({  
             url: '/aToB' ,  
             type: 'POST',  
             data: formData,  
             async: false,  
             cache: false,  
             contentType: false,  
             processData: false,  
             success: function (returndata) {  
          $("#test").append(returndata); 
             },  
             error: function (returndata) {  
                 alert("出错了");  
             }  
        }); 
  });
 </script>

后台:

@RequestMapping("/aToB")
 @ResponseBody
 public String aToBl(@RequestParam("testFile")MultipartFile multipartFile) throws Exception {

 InputStream inputStream = multipartFile.getInputStream();
  
  return "xxx";
 }

原文地址:https://www.cnblogs.com/SweetTooth/p/7867309.html