由于语言和场景不一样,网上的资料并不一定完美解决自己的问题。
参考链接:http://yunzhu.iteye.com/blog/2177923
花了点时间解决这个问题,在次记录一下。
Jq+asp.net 提交form表单。
Html+JS代码:
1 @******************************************************************************************************************@ 2 <form style="border:dashed" action="../News/TestForm" method="post" enctype="multipart/form-data" id="fromData"> 3 <div> 4 <label>填写</label><input type="text" name="text" /> 5 <textarea name="textarea"></textarea> 6 7 <input type="file" name="uploadfile" /> 8 9 <input type="button" name="submit11" id="submit11" value="提交" /> 10 </div> 11 </form> 12 <script src="~/js/jquery.min.js"></script> 13 <script src="~/js/jquery.form.js"></script> 14 <script type="text/javascript"> 15 $("#submit11").click(function () { 16 var formData = new FormData(document.getElementById("fromData")); 17 $.ajax({ 18 type: "post", 19 cache: true, 20 url: "../News/TestForm", 21 data: formData, 22 contentType: false, //一定要加这两行,不然报错Illegal invocation 23 processData: false, //一定要加这两行,不然报错Illegal invocation 24 success: function (data) { 25 alert(data); 26 } 27 }) 28 }); 29 </script> 30 @******************************************************************************************************************@
控制器代码:
1 public string TestForm(FormCollection form) 2 { 3 string text = form["text"]; 4 string textarea = form["textarea"]; 5 HttpPostedFileBase file = Request.Files["uploadfile"]; 6 string result = "YES"; 7 return result; 8 }