本文介绍了如果是ienumerable&lt; httppostedfilebase&gt;传递null错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在研究mvc 5项目,视图页面内有3个输入类型文件上传器。 这里如果我选择的只有一个fileUploader,另外两个正在传递null。如果null存在,我想避免保存。通过使用下面的代码,我该如何解决这个问题? 错误:对象引用没有设置为对象的实例。 请找到下面的图片,显示错误原因。 [ ^ ] 我的尝试: [HttpPost] public ActionResult PremiumUserRegistration(IEnumerable< HttpPostedFileBase> fileUpload) { IList< HttpPostedFileBase> ; list =(IList< HttpPostedFileBase>)fileUpload; HttpFileCollectionBase files = Request.Files; var httpPostedFile = HttpContext.Request.Files [ 0 ]; if (httpPostedFile.ContentLength!= 0 ) { for ( int i = 0 ; i < fileUpload.C​​ount(); i ++) { if (list [i] .ContentLength > 0 && i == 0 ) { HttpPostedFileBase file = files [i]; LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + _ + list [ I] .FileName); var path = Path.Combine(Server.MapPath( 〜/ UploadedImages / PremiUserRegistration / LogoImage /),LogoImage); file.SaveAs(path); } else if (list [i] .ContentLength > 0 && i == 1 ) { HttpPostedFileBase file = files [i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + _ + list [ I] .FileName); var path = Path.Combine(Server.MapPath( 〜/ UploadedImages / PremiUserRegistration / TitlePicture /),TitlePictureImage); file.SaveAs(path); } else if (list [i] .ContentLength > 0 && i == 2 ) { HttpPostedFileBase file = files [i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + _ + list [ I] .FileName); var path = Path.Combine(Server.MapPath( 〜/ UploadedImages / PremiUserRegistration / Brochure /),TitlePictureImage); file.SaveAs(path); } } } } 请帮助我... 解决方案 添加此行以验证 null value for(int i = 0; i< fileUpload.C​​ount(); i ++) { if(List [i]!= null ) { if(list [i] .ContentLength> 0&& i == 0) { HttpPostedFileBase file =文件[I]; LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName +_+ list [i] .FileName); var path = Path.Combine(Server.MapPath(〜/ UploadedImages / PremiUserRegistration / LogoImage /),LogoImage); file.SaveAs(path); } else if(list [i] .ContentLength> 0&& i == 1) { HttpPostedFileBase file = files [i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName +_+ list [i] .FileName); var path = Path.Combine(Server.MapPath(〜/ UploadedImages / PremiUserRegistration / TitlePicture /),TitlePictureImage); file.SaveAs(path); } else if(list [i] .ContentLength> 0&& i == 2) { HttpPostedFileBase file = files [i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName +_+ list [i] .FileName); var path = Path.Combine(Server.MapPath(〜/ UploadedImages / PremiUserRegistration / Brochure /),TitlePictureImage); file.SaveAs(path); } } } I am working on mvc 5 project, there is 3 input type file uploader inside view page.Here if I am selecting the only one fileUploader, another two are passing null. I want to avoid to save if null existing. By using below code, how can I solve that issue?error: "Object reference not set to an instance of an object."please find the below image, that showing error cause.[^]What I have tried:[HttpPost] public ActionResult PremiumUserRegistration(IEnumerable<HttpPostedFileBase> fileUpload) { IList<HttpPostedFileBase> list = (IList<HttpPostedFileBase>)fileUpload; HttpFileCollectionBase files = Request.Files; var httpPostedFile = HttpContext.Request.Files[0]; if (httpPostedFile.ContentLength != 0) { for (int i = 0; i < fileUpload.Count(); i++) { if (list[i].ContentLength > 0 && i == 0) { HttpPostedFileBase file = files[i]; LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/LogoImage/"), LogoImage); file.SaveAs(path); } else if (list[i].ContentLength > 0 && i == 1) { HttpPostedFileBase file = files[i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/TitlePicture/"), TitlePictureImage); file.SaveAs(path); } else if (list[i].ContentLength > 0 && i == 2) { HttpPostedFileBase file = files[i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/Brochure/"), TitlePictureImage); file.SaveAs(path); } } }}Please help me... 解决方案 add this line to validate null valuefor (int i = 0; i < fileUpload.Count(); i++) { if (List[i] != null) { if (list[i].ContentLength > 0 && i == 0) { HttpPostedFileBase file = files[i]; LogoImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/LogoImage/"), LogoImage); file.SaveAs(path); } else if (list[i].ContentLength > 0 && i == 1) { HttpPostedFileBase file = files[i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/TitlePicture/"), TitlePictureImage); file.SaveAs(path); } else if (list[i].ContentLength > 0 && i == 2) { HttpPostedFileBase file = files[i]; TitlePictureImage = Path.GetFileName(partnersVM.listPartnerVM.PartnerName + "_" + list[i].FileName); var path = Path.Combine(Server.MapPath("~/UploadedImages/PremiUserRegistration/Brochure/"), TitlePictureImage); file.SaveAs(path); } } } 这篇关于如果是ienumerable&lt; httppostedfilebase&gt;传递null错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 17:14