public ActionResult RemoveImage(string Id) { using (Image_context db = new Image_context()) { int id = Int32.Parse(Id); Image_base image = db.Images.SingleOrDefault(f => f.Id == id); string url = image.url; var img = new Image_base { Id = id }; if (System.IO.File.Exists(url)) System.IO.File.Delete(url); db.Images.Attach(image); db.Images.Remove(image); db.SaveChanges(); } return RedirectToAction("Index", "Home"); }
public ActionResult UploadImage(HttpPostedFileBase[] files) { if (files[0]!=null) { foreach (var file in files) { using (Image_context db = new Image_context()) { MemoryStream ms = new MemoryStream(); file.InputStream.CopyTo(ms); Image_base image = new Image_base { url = ServerPath + "\\images\\" + file.FileName, user_description = null, load_date = DateTime.Now.ToString(), change_date = DateTime.Now.ToString(), imgtype = file.ContentType }; db.Images.Add(image); db.SaveChanges(); System.IO.Directory.CreateDirectory(ServerPath + "\\images\\"); string path =ServerPath + "\\images\\" + file.FileName; FileStream newfile = new FileStream(path, FileMode.Create, FileAccess.Write); ms.WriteTo(newfile); newfile.Close(); Response.Write(true); } } } return RedirectToAction("Index", "Home"); }
public ActionResult GetSetComments(user_request request) { using (Image_context db = new Image_context()) { int id = Int32.Parse(request.Id); Image_base img = db.Images.SingleOrDefault(f => f.Id == id); StringBuilder sb = new StringBuilder(); if (request.Text == null) { if (img.user_description != null) { sb.AppendFormat("<li class=\"editable\" data-value=\"{0}\"> {1} </li>", id, img.user_description); Response.Write(sb.ToString()); } else { sb.AppendFormat("<li class=\"editable\" data-value=\"{0}\"> No Comments </li>", id); Response.Write(sb.ToString()); } } else { img.user_description = request.Text; img.change_date = DateTime.Now.ToString(); var original = db.Images.Find(img.Id); if (original != null) { original.change_date = img.change_date; original.user_description = img.user_description; db.SaveChanges(); } } return null; } }