public ActionResult Upload(HttpPostedFileBase file) { if (file != null) { Stream img = file.InputStream; BinaryReader br = new BinaryReader(img); byte[] bytes = br.ReadBytes((Int32)img.Length); Models.DB.Item obj = (Models.DB.Item)Session["PB"]; using (Models.DB.MVCEntities db = new Models.DB.MVCEntities()) { var imgObj = (from s in db.Item where s.itemID == obj.itemID select s).FirstOrDefault(); imgObj.itemID = obj.itemID; imgObj.itemName = obj.itemName; imgObj.imgdata = bytes; db.SaveChanges(); TempData["resultMessage"] = String.Format("item 「{0}」 image uploaded.", obj.itemName); return(RedirectToAction("Index")); } } else { TempData["ResultMessage"] = "upload null, please check"; return(RedirectToAction("Index")); } /*byte[] bytes; * using (BinaryReader br = new BinaryReader(file.InputStream)) * { * bytes = br.ReadBytes(file.ContentLength); * } * Models.DB.MVCEntities ent = new Models.DB.MVCEntities(); * ent.Item.Add(new Models.DB.Item { * imgdata = bytes * }); * ent.SaveChanges(); * TempData["ResultMessage"] = "upload success, please check"; * return RedirectToAction("Index");*/ /* //byte[] bytes = new byte[imgdata.ContentLength]; * Stream img = imgdata.InputStream; * BinaryReader br = new BinaryReader(img); * byte[] bytes = br.ReadBytes((Int32)img.Length); * //{ * //bytes = br.ReadBytes((Int64)imgdata.ContentLength); * //} * //string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); * Models.DB.Item obj = (Models.DB.Item)Session["PB"]; * using (Models.DB.MVCEntities db = new Models.DB.MVCEntities()) * * { * var imgObj = (from s in db.Item where s.itemID == obj.itemID select s).FirstOrDefault(); * imgObj.itemID = obj.itemID; * imgObj.itemName = obj.itemName; * imgObj.imgdata = bytes; * db.SaveChanges(); * TempData["resultMessage"] = String.Format("item 「{0}」 image uploaded.", obj.itemName); * return RedirectToAction("Index"); * }*/ }
public ActionResult Create(Models.DB.Item postback) { if (this.ModelState.IsValid) { using (Models.DB.MVCEntities db = new Models.DB.MVCEntities()) { db.Item.Add(postback); db.SaveChanges(); TempData["ResultMessage"] = String.Format("item 「{0}」 add successful with image", postback.itemName); return(RedirectToAction("Index")); } } ViewBag.ResultMessage = "Input error, Please input again!"; return(View(postback)); }
public ActionResult Edit(Models.DB.Item postback) { if (this.ModelState.IsValid) { using (Models.DB.MVCEntities db = new Models.DB.MVCEntities()) { var result = (from s in db.Item where s.itemID == postback.itemID select s).FirstOrDefault(); result.itemID = postback.itemID; result.itemName = postback.itemName; result.iDesc = postback.iDesc; result.itemType = postback.itemType; result.iStatus = postback.iStatus; result.iPrice = postback.iPrice; result.qty = postback.qty; db.SaveChanges(); TempData["ResultMessage"] = String.Format("item {0} edit complete", postback.itemName); return(RedirectToAction("Index")); } } else { return(View(postback)); } }