private void UploadFile(CrmCusDocumentModel model, List <HttpPostedFileBase> filesUpdoad) { if (filesUpdoad == null || !filesUpdoad.Any()) { return; } var type = model.GetType().ToString(); foreach (var file in filesUpdoad.Where(file => file != null && file.ContentLength > 0)) { try { var pathfoder = Path.Combine(Server.MapPath(@"~/" + DOCUMENT_PATH), model.CrmCusId.ToString("D6"), type, model.Id.ToString("D4")); if (!Directory.Exists(pathfoder)) { Directory.CreateDirectory(pathfoder); } var filePath = Path.Combine(pathfoder, Path.GetFileName(file.FileName)); file.SaveAs(filePath); var fileName = file.FileName; if (file.FileName.Length > 100) { var fileList = file.FileName.Split('.'); var typeDoc = fileList[fileList.Length - 1]; fileName = file.FileName.Substring(0, 95) + "." + typeDoc; } //save file to db var fileSave = new ServerFile { ObjectId = model.Id, ObjectType = type, // Path = string.Format("{0}/{1}", pathfoder, file.FileName), Path = string.Format("{0}/{1}/{2}/{3}/{4}", DOCUMENT_PATH, model.CrmCusId.ToString("D6"), type, model.Id.ToString("D4"), file.FileName), FileName = fileName, FileSize = file.ContentLength, FileMimeType = file.ContentType }; fileService.Insert(fileSave); } catch (Exception ex) { Logger.LogError(ex); } } }
public ActionResult Edit(CrmCusDocumentModel model) { model.FilesList = new List <ServerFile>(); if (!ModelState.IsValid) { var errors = ModelState.Where(n => n.Value.Errors.Count > 0).ToList(); return(Json(new { Message = Resources.Resource.CRM_EDIT_ERROR_MESSAGE_BLANK, Success = false, View = this.RenderPartialView("_TemplateEditView", model) }, JsonRequestBehavior.AllowGet)); } try { if (model.Id == 0) { model.CreatedById = CurrenUser.Id; model.Id = documentService.InsertModel(model); } else { model.ModifiedById = CurrenUser.Id; documentService.UpdateModel(model); } UploadFile(model, model.Uploads); return(Json(new { Message = @"Thành công", Success = true }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(Json(new { Message = Resources.Resource.CRM_EDIT_ERROR_MESSAGE, Success = false, View = this.RenderPartialView("_TemplateEditView", model) }, JsonRequestBehavior.AllowGet)); } }
public ActionResult Edit(long cusId, long id = 0) { var model = new CrmCusDocumentModel { CrmCusId = cusId, FilesList = new List <ServerFile>() }; var doc = documentService.GetModel(id); crmCustomer = crmCustomer ?? crmCustomerService.GetModelById(cusId); if (doc != null) { model = doc; model.FilesList = fileService.GetServerFile(id, new CrmCusDocumentModel().GetType().ToString()); } var value = new { Views = this.RenderPartialView("_TemplateEditView", model), Title = string.Format(@"{0} tài liệu {1} ", id > 0 ? "Sửa " : "Tạo ", crmCustomer.CompanyShortName), }; return(JsonResult(value, true)); }