使用Ajax时无法获取HttpPostedFileBase

时间:2015-09-05 04:43:13

标签: jquery ajax asp.net-mvc

我有一个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;" /> }

0 个答案:

没有答案