public ActionResult Remove(string[] fileNames) { PictureRepository picRep = new PictureRepository(); int NewPicID = picRep.GetLastPictureId(Convert.ToInt32(HttpContext.Session["ClientId"])); // The parameter of the Remove action must be called "fileNames" foreach (var fullName in fileNames) { var fileName = Path.GetFileName(fullName); var extention = fileName.Substring(fileName.IndexOf('.')); var newfilename = NewPicID.ToString() + extention; // Check if Path exsits string serverpath = Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"); if (!Directory.Exists(serverpath)) { Directory.CreateDirectory(serverpath); } var physicalPath = Path.Combine(Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"), newfilename); // TODO: Verify user permissions if (System.IO.File.Exists(physicalPath)) { // The files are not actually removed in this demo System.IO.File.Delete(physicalPath); picRep.RemovePicture(NewPicID); } } // Return an empty string to signify success return Content(""); }
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments) { PictureRepository picRep = new PictureRepository(); Functions functions = new Functions(); // The Name of the Upload component is "attachments" int NewPicID = picRep.GetLastPictureId(Convert.ToInt32(HttpContext.Session["ClientId"])); NewPicID++; Picture ins = new Picture(); foreach (var file in attachments) { // Some browsers send file names with full path. This needs to be stripped. var fileName = Path.GetFileName(file.FileName); var extention = fileName.Substring(fileName.IndexOf('.')); var newfilename = NewPicID.ToString() + extention; // Check if Path exsits string serverpath = Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"); if (!Directory.Exists(serverpath)) { Directory.CreateDirectory(serverpath); } var physicalPath = Path.Combine(Server.MapPath("~/Images/Client/" + HttpContext.Session["ClientId"].ToString() + "/Ads"), newfilename); //...Resize.. Image original = Image.FromStream(file.InputStream, true, true); Image resized = functions.ResizeImage(original, new Size(500, 500)); resized.Save(physicalPath, ImageFormat.Jpeg); string finalpath = physicalPath.ToString(); finalpath = finalpath.Substring(finalpath.IndexOf("Images")); finalpath = finalpath.Replace('\\', '/'); //...Save In DB... ins.PicUrl = Constants.HTTPPath + finalpath; ins.ClientId = Convert.ToInt32(HttpContext.Session["ClientId"]); ins = picRep.InsertPicture(ins); } // Return an empty string to signify success if (ins.PictureId != 0) return Json(new { status = ins.PictureId.ToString() }, "text/plain"); else return Json(new { status = "0" }, "text/plain"); }