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
});
});
复制代码

posted @ 2020-07-17 13:55  乌卡拉卡  阅读(470)  评论(0编辑  收藏  举报