首页 > 代码库 > asp.net mvc表单异步提交

asp.net mvc表单异步提交

html代码:

@using (Html.BeginForm("xx", "xx", FormMethod.Post, new { enctype = "multipart/form-data", id = "form1", @class = "form-horizontal", role = "form" }))
{

<div>
@Html.DropDownList("cate", ViewData["cate"] as List<SelectListItem>, new { @class = "form-control", onchange = "SelectChange(this)", id = "select1" })
</div>

<div>

<input type="file" id=‘picture‘  accept=‘image/png,image/jpeg‘>

</div>

<div>

<button type="button" class="btn1">提交</button>

</div>

}

js代码:

$(".btn1").onclick(function(){

 var formData = http://www.mamicode.com/new FormData();

var cat=$("#select1").val();

formData.append("cate", cat);

formData.append("img",document.getElementById(‘picture‘).files[0]);

//var xhr = new XMLHttpRequest();
//xhr.open("post", ‘@Url.Action("xx", "xx")‘);
//xhr.addEventListener("load", uploadComplete, false);

////formData.delete(imgID);
//xhr.send(formData);
//return false;

$.ajax({
url: $("#form1").attr("action"),
type: $("#form1").attr("method"),
cache: false,
data: formData,
processData: false,
contentType: false,
success: function (result) {
}
});

})

function uploadComplete(evt) {

var d = evt.target.responseText.toString();
var data = http://www.mamicode.com/JSON.parse(d);

}

 

Control代码:

var ca = Request.Params["cate"];

//HttpFileCollectionBase files = Request.Files;

HttpPostedFileBase fileimg = Request.Files[0];

 

asp.net mvc表单异步提交