视图:
$(function () {
$("#sub").click(function () {
var fd = new FormData();
fd.append("filel", $("#file").get(0).files[0]);
$.ajax({
url: "@Url.Content("~/Home/ImportFile")",
type: "post",
contentType: false, //文件类型(为false时,不采用系统默认的文件类型,任意类型)
processData: false, //默认为false,当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
data: fd,
success: function (e) {
if (e=="1") {
alert("文件上传成功!");
}
else{
alert("文件上传失败!");
}
}
});
});
});
控制器:
public ActionResult ImportFile(HttpPostedFileBase filel)
{
try
{
string path = Server.MapPath("~/App_Data") +"/"+ filel.FileName;
filel.SaveAs(path);
return Content("1");
}
catch (Exception)
{
return Content("0");
}
}