public int UploadImageInDataBase(HttpPostedFileBase file, ExtlinkViewModel ExtlinkViewModel) { ExtlinkViewModel.Img = ConvertToBytes(file); var extlink = new Extlink { Title = ExtlinkViewModel.Title, UrlLink = ExtlinkViewModel.UrlLink, Created = ExtlinkViewModel.Created, Author = ExtlinkViewModel.Author, ImageId = ExtlinkViewModel.ImageId, NavbarId = ExtlinkViewModel.NavbarId, Img = ExtlinkViewModel.Img, IsExternal = ExtlinkViewModel.IsExternal }; db.Extlinks.Add(extlink); int i = db.SaveChanges(); if (i == 1) { return(1); } else { return(0); } }
public ActionResult EditPost(int?id, ExtlinkViewModel model) { ViewBag.PageEdit = true; ViewBag.Class = "admin"; ViewBag.PdfPanelActive = "active"; ViewBag.Footer = false; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var UrlItem = db.Extlinks.Find(id); if (UrlItem == null) { return(HttpNotFound()); } if (Request.Files["fileImg"] != null) { HttpPostedFileBase file = Request.Files["fileImg"]; UrlItem.Img = ConvertToBytes(file); if (TryUpdateModel(UrlItem, "", new string[] { "Title", "UrlLink", "Created", "Author", "ImageId", "NavbarId", "Img", "IsExternal" })) { ; } } else { if (TryUpdateModel(UrlItem, "", new string[] { "Title", "UrlLink", "Created", "Author", "ImageId", "NavbarId", "IsExternal", })) { ; } } db.Entry(UrlItem).State = EntityState.Modified; int i = db.SaveChanges(); byte[] ConvertToBytes(HttpPostedFileBase image) { byte[] imageBytes = null; var reader = new System.IO.BinaryReader(image.InputStream); imageBytes = reader.ReadBytes((int)image.ContentLength); return(imageBytes); } return(RedirectToAction("Index")); }
public ActionResult Create(ExtlinkViewModel model) { ViewBag.PageEdit = true; ViewBag.Class = "admin"; ViewBag.ExtlinkPanelActive = "active"; ViewBag.Footer = false; HttpPostedFileBase file = Request.Files["upload"]; ExtlinkRepository service = new ExtlinkRepository(); int i = service.UploadImageInDataBase(file, model); if (i == 1) { return(RedirectToAction("Index")); } return(View(model)); }