Пожалуйста, измените это, переместите строку чтения кода из потока вверх
if (postedFile != null)
{
var a = new byte[postedFile.ContentLength];
postedFile.InputStream.Read(a, 0, postedFile.ContentLength);
article.image = Convert.ToBase64String(a);
}
Обновлен:
Я пытался воспроизвести исходный код на моей стороне, он работал хорошо.
Вы установили new {enctype="multipart/form-data"}
для своей формы?
[HttpPost]
//[ValidateAntiForgeryToken]
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];
postedFile.InputStream.Read(a, 0, postedFile.ContentLength);
article.image = Convert.ToBase64String(a);
//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();
TempData["Image"] = article.image;
}
return RedirectToAction("Index");
}
return View(article);
}
Create.cshtml file
@using(Html.BeginForm("Create","Feedback",FormMethod.Post,new {enctype="multipart/form-data"}))
{
<input type="file" name="postedFile"/>
<input type="submit" value="Save"/>
}
Файл Index.cshtml
@{
var imgSrc = string.Format("data:image/gif;base64,{0}", TempData["Image"]);
}
<img src="@imgSrc"/>