public async Task <IActionResult> SaveItem(IList <IFormFile> files, CategoriesAblums model) { string ControllerName = this.ControllerContext.RouteData.Values["controller"].ToString(); int IdDC = Int32.Parse(MyModels.Decode(model.Ids, API.Models.Settings.SecretId + ControllerName).ToString()); CategoriesAblumsModel data = new CategoriesAblumsModel() { Item = model }; if (ModelState.IsValid) { if (model.Id == IdDC) { model.CreatedBy = int.Parse(HttpContext.Request.Headers["Id"]); model.ModifiedBy = int.Parse(HttpContext.Request.Headers["Id"]); var Cat = CategoriesAblumsService.SaveItem(model); foreach (IFormFile source in files) { string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Trim('"'); filename = this.EnsureCorrectFilename(filename); using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename))) { await source.CopyToAsync(output); Ablums alb = new Ablums() { CatId = Convert.ToInt32(Cat.N), CreatedBy = model.CreatedBy, ModifiedBy = model.ModifiedBy, Images = this.GetPathAndFilename(filename) }; AblumsService.SaveItem(alb); } } if (model.Id > 0) { TempData["MessageSuccess"] = "Cập nhật thành công"; } else { TempData["MessageSuccess"] = "Thêm mới thành công"; } return(RedirectToAction("Index")); } } return(View(data)); }
public async Task <IActionResult> UPloadFiles() { var form = Request.Form; CategoriesAblums model = new CategoriesAblums() { Id = Convert.ToInt32(form["id"]), Ids = form["ids"], Status = Convert.ToBoolean(form["status"]), Description = form["desc"], Title = form["ten"], Images = form["Images"], Alias = form["Alias"], Featured = Boolean.Parse(form["Featured"]), ParentId = int.Parse(form["ParentId"]), }; //ICollection<string> col = form.Keys; var datafiles = JsonConvert.DeserializeObject <List <Ablums> >(form["data"]); string ControllerName = this.ControllerContext.RouteData.Values["controller"].ToString(); int IdDC = Int32.Parse(MyModels.Decode(model.Ids, API.Models.Settings.SecretId + ControllerName).ToString()); //CategoriesAblumsModel data = new CategoriesAblumsModel() { Item = model }; if (ModelState.IsValid) { if (model.Id == IdDC) { model.CreatedBy = int.Parse(HttpContext.Request.Headers["Id"]); model.ModifiedBy = int.Parse(HttpContext.Request.Headers["Id"]); var Cat = CategoriesAblumsService.SaveItem(model); foreach (IFormFile source in form.Files) { string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Trim('"'); filename = this.EnsureCorrectFilename(filename); using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename))) { await source.CopyToAsync(output); Ablums alb = new Ablums(); alb.CatId = Convert.ToInt32(Cat.N); alb.CreatedBy = model.CreatedBy; alb.ModifiedBy = model.ModifiedBy; alb.Images = filename; AblumsService.SaveItem(alb); } } if (datafiles != null) { for (var i = 0; i != datafiles.Count(); i++) { var item = datafiles[i]; if (item.Deleted == true) { var fullPath = GetPathAndFilename(item.Images.ToString()); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } Ablums alb = new Ablums() { Id = Convert.ToInt32(item.Id), ModifiedBy = model.ModifiedBy }; AblumsService.DeleteItem(alb); } } } TempData["MessageSuccess"] = "Cập nhật thành công"; return(RedirectToAction("Index")); } } return(Json("asdfsd")); }