public ActionResult Create(tVoteProject tVoteProjects, HttpPostedFileBase fVP_Imagefile) { if (ModelState.IsValid) { if (fVP_Imagefile != null && fVP_Imagefile.ContentLength > 0) { //檔案上傳 string strPath = Request.PhysicalApplicationPath + @"fVP_Imagefile\" + fVP_Imagefile.FileName; fVP_Imagefile.SaveAs(strPath); tVoteProjects.fVP_Imagefile = fVP_Imagefile.FileName; //將上傳的圖轉成二進位 var imgSize = fVP_Imagefile.ContentLength; byte[] imgByte = new byte[imgSize]; fVP_Imagefile.InputStream.Read(imgByte, 0, imgSize); tVoteProjects.fVP_Image = imgByte; db.tVoteProject.Add(tVoteProjects); db.SaveChanges(); return(RedirectToAction("Index", "Vote")); } else { ViewBag.message = "請選擇圖檔!!"; } } ViewBag.datas = db.tVoteProject.ToList(); return(View()); }
//回傳二進位圖 public ActionResult GetImageByte(int id) { tVoteProject tVoteProjects = db.tVoteProject.Find(id); byte[] img = tVoteProjects.fVP_Image; return(File(img, "image/jpeg")); }
public ActionResult DeleteConfirmed(int id) { tVoteProject tVoteProjects = db.tVoteProject.Find(id); db.tVoteProject.Remove(tVoteProjects); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tVoteProject tVoteProjects = db.tVoteProject.Find(id); if (tVoteProjects == null) { return(HttpNotFound()); } return(View(tVoteProjects)); }