本文介绍了Asp.net MVC-fileupload图像值在httppost方法中显示空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个可以上传图像和其他表单值的MultipartForms.While,表单值通过FormCollection属性正确接收,而Upload文件总是在HttpPostedFileBase属性中显示空值我通过论坛,但我无法得到哪里错了。在这里,我做了什么请经历它并说出了什么错了。谢谢朋友。 我尝试过: cshtml: @using(Html.BeginForm(Create,StaffRegistration,FormMethod.Post,new {enctype = multipart / form-data})) { < input type =filename =StaffImageid =StaffImage/> } 控制器: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection,HttpPostedFileBase File) { try { // TODO:在这里添加插入逻辑 StaffRegistration StaffReg = new StaffRegistration(); StaffReg.FirstName = collection [FirstName]。ToString(); StaffReg.LastName = collection [LastName]。ToString(); StaffReg.DateOfBirth = DateTime.Parse(collection [DateofBirth]); StaffReg.Nationality = collection [ 国籍]。ToString(); StaffReg.Gender = collection [Gender]。ToString(); StaffReg.MaritalStatus = collection [MaritalStatus]。ToString(); StaffReg.BloodGroup = collection [BloodGroup]。ToString(); StaffReg.StaffName = collection [StaffName]。ToString(); StaffReg.MiddleName = collection [MiddleName]。ToString(); HttpPostedFileBase文件= Request.Files [StaffImage]; StaffRegistrationBusSer StaffRegBusSer = new StaffRegistrationBusSer(); StaffRegBusSer.AddStaffReg(StaffReg,文件); 返回RedirectToAction(索引); } DataLayer: public bool AddStaffRegistraiton(StaffRegistration staffRegistration,HttpPostedFileBase File) { staffRegistration.StaffImage = ConvertToByte(文件); usi ng(SqlConnection Con = new SqlConnection(ConnectionString)) { SqlParameter paramImage = new SqlParameter(); paramImage.ParameterName =@ StaffImage; paramImage.Value = staffRegistration.StaffImage; Cmd.Parameters.Add(paramImage); Con.Open(); Cmd.ExecuteNonQuery(); } 返回true; } ConvertToByte函数: public byte [] ConvertToByte(HttpPostedFileBase Image) { byte [] imagebyte = null; BinaryReader Reader = new BinaryReader(Image.InputStream); imagebyte = Reader。 ReadBytes((int)Image.ContentLength); 返回imagebyte; } 解决方案 I have a MultipartForms in Which I Could upload an Image and other form values.While, the form values are rightfully received through the FormCollection Property whereas the Upload file always shows the null value in HttpPostedFileBase Property.I go through forums but I couldn't get Where went Wrong. Here, Is What I done Please go through it and Said what Went Wrong.Thanks friend.What I have tried:cshtml: @using (Html.BeginForm("Create", "StaffRegistration", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="StaffImage" id="StaffImage" /> } Controller: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection,HttpPostedFileBase File) { try { // TODO: Add insert logic here StaffRegistration StaffReg = new StaffRegistration(); StaffReg.FirstName = collection["FirstName"].ToString(); StaffReg.LastName = collection["LastName"].ToString(); StaffReg.DateOfBirth = DateTime.Parse(collection["DateofBirth"]); StaffReg.Nationality = collection["Nationality"].ToString(); StaffReg.Gender = collection["Gender"].ToString(); StaffReg.MaritalStatus = collection["MaritalStatus"].ToString(); StaffReg.BloodGroup = collection["BloodGroup"].ToString(); StaffReg.StaffName = collection["StaffName"].ToString(); StaffReg.MiddleName = collection["MiddleName"].ToString(); HttpPostedFileBase file = Request.Files["StaffImage"]; StaffRegistrationBusSer StaffRegBusSer = new StaffRegistrationBusSer(); StaffRegBusSer.AddStaffReg(StaffReg,file); return RedirectToAction("Index"); }DataLayer: public bool AddStaffRegistraiton(StaffRegistration staffRegistration,HttpPostedFileBase File) { staffRegistration.StaffImage = ConvertToByte(File); using(SqlConnection Con = new SqlConnection(ConnectionString)) {SqlParameter paramImage = new SqlParameter(); paramImage.ParameterName = "@StaffImage"; paramImage.Value = staffRegistration.StaffImage; Cmd.Parameters.Add(paramImage); Con.Open(); Cmd.ExecuteNonQuery(); } return true; }ConvertToByte function: public byte[] ConvertToByte(HttpPostedFileBase Image) { byte[] imagebyte = null; BinaryReader Reader = new BinaryReader(Image.InputStream); imagebyte = Reader.ReadBytes((int)Image.ContentLength); return imagebyte; } 解决方案 这篇关于Asp.net MVC-fileupload图像值在httppost方法中显示空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 11:44