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 = 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 = JSON.parse(d);

}

 

Control代码:

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

//HttpFileCollectionBase files = Request.Files;

HttpPostedFileBase fileimg = Request.Files[0];

 

转载于:https://www.cnblogs.com/ping2015/p/6122616.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页