被這個問題困擾了很久。。。真是醉了javascript
借用了jquery 的插件,jquery-form.js,連接:http://jquery.malsup.com/form/#downloadhtml
js代碼(options包含不少參數,能夠進行設置)java
<script type="text/javascript"> function save(){ var options = { success:function(data){ if(data.success==true) { alert("保存成功"); } } } // 當沒有選擇上傳文件,若是還想正確提交時必須作如下處理 if($("#pdfInput").val()==""){ $("#pdfInput").remove(); $("#uploadForm").attr("enctype", "application/x-www-form-urlencoded"); //或者不用remove()就設爲disabled也能夠 //$("#pdfInput").attr("disabled", "disabled"); } $('#uploadForm').ajaxSubmit(options); } </script>
Form表單jquery
<form action="${ctx}/StdManage/standardsUpdate" id="uploadForm" method="post" enctype="multipart/form-data"> <input type="text" name="id"> <input type="file" name="stdFile" id="pdfInput"> </form> <button type="button" onclick="save()">save</button>
Controllerweb
@Controller("/StdManage") public class StdManageController{ @RequestMapping("standardsUpdate") public void standardsUpdate(StandardWithBlobs standardWithBlobs) { System.out.println("id:" + standardWithBlobs.getId()); } }
StandardWithBlobsajax
package whu.edu.irlab.model; import org.springframework.web.multipart.MultipartFile; public class StandardWithBLOBs extends Standard{ private String entities; private String possibleEntities; private MultipartFile stdFile; getter and setter ... }