【发布时间】:2019-03-07 10:36:27
【问题描述】:
我尝试在我的数据库中保存图片类型String,但它总是给我System.Web.HttpPostedFileWrapper。我不明白这里有什么问题
我想创建一个包含标题、描述、图片及其类别的新产品。当我通过 create 发布数据时,它会保存数据但不显示 image 并且当我检查数据库图片字段时,我发现图像的值是 HttpPostedFileWrapper 而不是 p.png 或 product.jpg –
这是控制器:
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Create")]
public ActionResult Create([Bind(Include = "Ida,description,image,Userid,Idc,titre")] Article article,HttpPostedFileBase postedFile)
{
if (ModelState.IsValid)
{
if (postedFile != null)
{
var a =new byte[postedFile.ContentLength] ;
article.image = Convert.ToBase64String(a);
postedFile.InputStream.Read(a, 0, postedFile.ContentLength);
}
db = new IdentityDBEntities2();
// Add article to database
article.UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
article.Idc = Convert.ToInt32(Request["Idc"]);
db.Articles.Add(article);
ViewBag.Idcc = new SelectList(db.Categories, "Id", "libelle");
db.SaveChanges();
return RedirectToAction("Index");
}
return View(article);
}
【问题讨论】:
-
请尝试详细说明您的问题。什么是“图片”?你到底想做什么?另请注意,article.image 始终是转换为字符串的空字节数组或其默认值。
-
你好图片是数据库中的图片,我想在 ~/Content/img 文件夹中显示图片。
-
如果您的“编辑”是可行的解决方案,请将其移至下面的答案。这有助于更清楚地说明文本的哪一部分是问题,哪一部分是自我回答。
-
好的兄弟,谢谢你的通知
标签: asp.net asp.net-mvc entity-framework