entity framework5代码首先添加图像
我正在用mvc4和entity framework5编写一个非常小的应用程序。
我想为产品添加产品,商店和图像。
我有一个模特
[Table("CatalogItem")] public class CatalogItemModel { [Key] public int CatalogItemId { get; set; } public string Description { get; set; } public double Price { get; set; } public int ProductCount { get; set; } public string Size { get; set; } public string Sku { get; set; } [Column(TypeName = "image")] public byte[] Image { get; set; } [Display(Name = "Display Catalog Item")] public bool DisplayItem { get; set; } }
我的控制器。 这永远不会受到打击。
[HttpPost] public ActionResult Create(CatalogItemModel catalogitemmodel) { if (ModelState.IsValid) { db.CatalogItemModels.Add(catalogitemmodel); db.SaveChanges(); return RedirectToAction("Index"); } return View(catalogitemmodel); }
我的观点形成了
当我尝试在我的文件输入中发布带有图像的新目录时,它会引发错误
输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符。
尝试解决这个问题:
1。 更换
with
2。 在控制器中:
[HttpPost] public ActionResult Create(CatalogItemModel catalogitemmodel, HttpPostedFileBase ImageFile) { using (var ms = new MemoryStream()) { ImageFile.InputStream.CopyTo(ms); catalogitemmodel.Image = ms.ToArray(); } if (ModelState.IsValid) { db.CatalogItemModels.Add(catalogitemmodel); db.SaveChanges(); return RedirectToAction("Index"); } return View(catalogitemmodel); }