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];

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM