MVC使用Ajax.BeginForm上傳文件時HttpPostedFileBase file為null,Request.Files獲取不到文件,
把頁面中存在jquery.unobtrusive-ajax.js給注釋后掉后就正常。
前端代碼:
@using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
選擇文件:
<
input
type
=
"file"
name
=
"uploadFile"
/>
<
input
type
=
"submit"
/>
}
后端代碼:
[HttpPost]
public
ActionResult Upload(HttpPostedFileBase uploadFile)
{
if
(uploadFile !=
null
&& uploadFile.ContentLength > 0)
{
string
fileName = WebSecurity.CurrentUserId + Path.GetExtension(uploadFile.FileName)
string
direction = Server.MapPath(
"~/UpLoad/Companys/logo/"
);
string
destinationPath = Path.Combine(direction, fileName);
if
(!Directory.Exists(direction))
{
Directory.CreateDirectory(direction);
}
uploadFile.SaveAs(destinationPath);
}
return
View();
}