【发布时间】:2014-08-30 19:52:43
【问题描述】:
将图像从表单传递到我的控制器帖子。它被捕获为HttpPostedFileBase。不过,我想对图像进行一些验证。
我想在保存文件之前强制限制分辨率大小,但因为它是HttpPostedFileBase,所以我不能。有没有办法将其转换为 Image 属性或任何其他方式。
这是我的控制器:
[HttpPost]
public ActionResult BannerEditorEdit([Bind(Include = "ID,title,subTitle,imgPath,startBanner")]HttpPostedFileBase photo, BannerEditor bannerEditor)
{
if (ModelState.IsValid)
{
if (photo != null)
{
string basePath = Server.MapPath("~/Content/Images");
var supportedTypes = new[] { "jpg", "jpeg", "png", "PNG", "JPG", "JPEG" };
var fileExt = System.IO.Path.GetExtension(photo.FileName).Substring(1);
if (!supportedTypes.Contains(fileExt))
{
ModelState.AddModelError("photo", "Invalid type. Only the following types (jpg, jpeg, png) are supported.");
return View();
}
photo.SaveAs(basePath+ "//" + photo.FileName);
bannerEditor.imgPath = ("/Content/Images/" + photo.FileName);
}
else
{
ModelState.AddModelError("photo", "Must supply a Banner imgage");
}
db.Entry(bannerEditor).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("BannerEditorIndex");
}
return View(bannerEditor);
}
【问题讨论】:
标签: c# image asp.net-mvc-5 converter