如果是ienumerable< httppostedfilebase>传递null错误。 [英] If ienumerable<httppostedfilebase> passing null error.
问题描述
我正在研究mvc 5项目,视图页面内有3个输入类型文件上传器。
这里如果我选择的只有一个fileUploader,另外两个正在传递null。如果null存在,我想避免保存。通过使用下面的代码,我该如何解决这个问题?
错误:对象引用没有设置为对象的实例。
请找到下面的图片,显示错误原因。
[ ^ ]
我的尝试:
< pre lang =c#> [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);
}
}
}
}
请帮助我...
添加此行以验证null
value
for(int i = 0; i< fileUpload.Count(); 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 validatenull
value
for (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< httppostedfilebase>传递null错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!