[英]image upload and image.ContentLenght > 0
我在对象街上有一些照片。 我正在尝试以与创建新街道对象相同的形式实施上传。 因此,图像与字段的MVC Web表单
现在,在create.cshtml页面上,我具有带StreetName
和StreetNumber
字段的表单,并且
<input type="file" name="postedImages" />
<input type="file" name="postedImages" />
<input type="file" name="postedImages" />
提交此表单后,我会将这些数据发送到StreetController进行进一步处理
[HttpPost]
public ActionResult Create(StreetViewModel newData, IEnumerable<HttpPostedFileBase> postedImages)
{
//create object and save stretname and streetnumber
//process posted images
foreach(var image in postedImages)
{
//this is where error occured if I post only one or two images from my view
if(image.ContentLength >0)
{
//process and save images
}
}
}
如您所见,如果我张贴了Web表单上提供的确切数量的图像,则一切都很好,但是如果我发送1或2张图像,则会发生错误。
如何解决呢? 谢谢
在访问ContentLength
属性之前添加null
检查
if((image!=null) && (image.ContentLength >0))
{
//process and save images
}
foreach(var image in postedImages.Where(pi => pi != null))
您可以执行以下操作:
If(image != null)
if(image.ContentLength > 0)
{
//code
}
在获取属性之前,请检查其null是否是错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.