我有一个Ajax.BeginForm()
和一个<input> type="File"
当我使用Html.BeginForm
并提交此内容时。 HttpPostedFileBased
不为空。
但是,当我使用Ajax
时,HttpPostedFileBased
为空
这是我的代码
//if file is exist
if (avatarFile != null)
{
string extensionName = System.IO.Path.GetExtension(avatarFile.FileName).ToLower();
List<string> imageExtension = new List<string>() { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tif", ".tiff" };
foreach (var ex in imageExtension)
{
//if file is image file type
if (extensionName.Equals(ex))
{
string path = Server.MapPath("~/Files/Images/Avatars/" + avatarFile.FileName);
avatarFile.SaveAs(path);
var user = db.AspNetUsers.Find(User.Identity.GetUserId());
if (user != null)
{
user.Avatar = "../../Files/Images/Avatars/" + avatarFile.FileName;
db.SaveChanges();
}
}
}
}
return RedirectToAction("Profile", new { UserName = User.Identity.Name });
查看
@using (Ajax.BeginForm("UploadFile","Account",null, new AjaxOptions() { HttpMethod = "POST", LoadingElementId = "loadWhenChange" }, new { id = "formChangeAvatar", enctype = "multipart/form-data" }))
{
<input type="file" id="avatarFile" name="avatarFile" style="display:none;" />
}