gpt4 book ai didi

c# - 图片上传和image.Content Length > 0

转载 作者:太空宇宙 更新时间:2023-11-03 19:21:30 25 4
gpt4 key购买 nike

我有对象街道,上面有一些照片。我正在尝试以与创建新街道对象相同的形式实现上传。所以想象一个带有字段的 mvc web 表单

  • 街道名称
  • 街号
  • 照片1
  • 照片2
  • 照片3

现在,在 create.cshtml 页面上,我有包含 StreetNameStreetNumber 字段和

的表单
<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
}
}

}

正如您可以在我的评论行中看到的那样,如果我发布网络表单上提供的确切数量的图像,一切都很好,但如果我发送 1 或 2 张图像,则会发生错误。

如何解决?谢谢

最佳答案

在访问 ContentLength proeprty 之前添加一个 null 检查

if((image!=null) && (image.ContentLength >0))
{
//process and save images
}

关于c# - 图片上传和image.Content Length > 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11849231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com