@using( HTML.BeginForm("Create","ProductManager",FormMethod.Post,new{enctype = "multipart/form-data"})){ <ul> .... <li> @HTML.LabelFor(model => model.ProductimagePath,"Avatar") <input type="file" ID="ProductAvatar" name="ProductAvatar" /> @HTML.HIDdenFor(model => model.ProductimagePath,new { ID = "AvatarHIDdenFIEld"}) </li> <li> @HTML.LabelFor(model => model.Productname,"Product name") @HTML.EditorFor(model => model.Productname) </li> ..... </ul>}[httpPost] public ActionResult Create( FormCollection collection,httpPostedfileBase avatar) { string file = collection["ProductAvatar"]; var avatars = avatar; }
从调试我发现httpPostedfileBase返回null.集合中的其他表单数据成功发布.只有图像没有发布.我似乎无法从FormCollection或httpPostedfileBase访问ProductAvatar,似乎它甚至没有发布
我怎样才能解决这个问题呢?
解决方法 您必须使用将httpPostedfile参数的名称更改为表单上输入文件的相同名称,或者您也可以使用Request.files并通过输入文件的name属性获取,尝试以下 *** 作:[httpPost]public ActionResult Create(FormCollection collection){ httpPostedfileBase file = Request.files["ProductAvatar"]; if (file.ContentLength > 0) { file.SaveAs(/* path */); } // othyer tasks return RedirectToAction("Index");}
name属性是浏览器在提交时在发布/获取表单上发送的内容.
总结以上是内存溢出为你收集整理的c# – HttpPostedFileBase始终返回null,并且不会发布图像全部内容,希望文章能够帮你解决c# – HttpPostedFileBase始终返回null,并且不会发布图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)