1、html代码:
<form id="postForm"> 文件名:<input type="text" name="filename" /> 文件: <input type="file" name="my_file" /> <input type="button" value="提交" onclick="btnPost()" /> </form>
2、jquery代码:
<script> function btnPost() { var formData = new FormData($("#postForm")[0]); $.ajax({ url: "/Home/Post", data: formData, type: "POST", async: false, contentType: false, processData: false, success: function (msg) { }, error: function (e) { } }); } </script>
注意:
contentType: false,------------>为了上传文件,具体百度有,
processData: false ----------->processData 默认为false,当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
3、cs代码:
public ActionResult Post() { var name = Request.Form["filename"]; HttpPostedFileBase myFile = Request.Files["my_file"]; ....... }