jQuery使用FormData上传文件
https://www.cnblogs.com/insus/p/4648289.html
控制器中,创建两个Action:
public ActionResult FilejQLoad()
{
return View();
}
public ActionResult Uf(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Temp"), fileName);
file.SaveAs(path);
}
return new ContentResult();
}
jQuery代码:
$(':button').click(function () {
var formData = new FormData($('form')[0]);
$.ajax({
url: 'Uf',
type: 'POST',
xhr: function () {
return $.ajaxSettings.xhr();
},
success: function (data, textStatus) {
alert("file success uploaded.");
location.reload();
},
data: formData,
cache: false,
contentType: false,
processData: false
});
});
复制代码