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