我尝试在String
我的数据库中保存图片类型,但它总是给我System.Web.HttpPostedFileWrapper
. 我不明白这里有什么问题
我想创建一个包含标题、描述、图像及其类别的新产品。当我通过创建发布数据时,它会保存数据但不显示图像,当我检查数据库图片字段时,我发现图像的值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);
}
您的postedfile参数是一个HttpPostedFileBase类型的参数。您必须在客户端上保存文件的完全限定名称,而不是直接保存此参数。试试这个:
article.image = postedFile.FileName;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句