public JsonResult AddClientDocument(ClientDocument clientDoc, Document file) { var result = false; string UserName = System.Web.HttpContext.Current.User.Identity.Name; DateTime Now = DateTime.Now; string newDir = Server.MapPath("~/Storage/ClientDocument/"); //-- string noFileFoundMessage; //if (ModelState.IsValid) //{ if ((file.DocumentID == Guid.Empty) && (file.FileName != null)) //-- NEW == we need to Add the Document { //1. Prep Document + Save File //===================================================================== file.DocumentID = Guid.NewGuid(); file.DateImported = Now; file.InputDate = Now; //===================================================================== //2. Prep+Move The Actual File from Temp //===================================================================== string StorageFolder = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["StorageFolder"]); string fullFileName = System.IO.Path.Combine(StorageFolder, file.FileName); //FileName => [email protected] if (System.IO.File.Exists(fullFileName)) { try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullFileName); fileInfo.MoveTo(newDir + fileInfo.Name); //====================================================================== //3.Update Document Table with updated path and doc info //====================================================================== file.DocumentPath = fileInfo.DirectoryName; //Get the new directory path after move file.FileExtension = fileInfo.Extension; file.InputBy = UserName; //Now save using (this.UoW) { this.UoW.Document.Insert(file); //====================================================================== //4. Now the Document is saved... Update the Client-Document Table //====================================================================== //var vendorDocument = new ClientDocument(); if (clientDoc.ClientDocumentID == Guid.Empty) { clientDoc.ClientDocumentID = Guid.NewGuid(); clientDoc.DocumentID = file.DocumentID; //vendorDocument.ClientID = clientDoc.ClientID; clientDoc.InputBy = UserName; clientDoc.InputDate = Now; this.UoW.ClientDocuments.Insert(clientDoc); } //Now Commit result = this.UoW.Commit() > 0; //if successful delete the session key if (result == true) { var key = fileInfo.Name.Substring(fileInfo.Name.IndexOf('@') + 1); if (System.Web.HttpContext.Current.Session[key] != null) { System.Web.HttpContext.Current.Session[key] = null; } } } return Json(new { Success = result }); } catch (Exception e) { //Something went wrong, so we need to clean up string fn = newDir + file.FileName; if (System.IO.File.Exists(fn)) { //Delete so we don't have any orphan file lying around System.IO.File.Delete(fn); } //throw; } } else //We didn't find any file to save... { noFileFoundMessage = "Didn't find any valid document to save. Please upload a valid file and then hit the Save button"; return Json(new { Success = result , errMsg = noFileFoundMessage}); } } return Json(new { Success = result }); //} //else //{ // return Json(new { Success = result, Message = "Invalid Model" }); //} }
public JsonResult EditClientDocument(ClientDocument clientDoc) { bool result = false; string UserName = System.Web.HttpContext.Current.User.Identity.Name; DateTime Now = DateTime.Now; if (ModelState.IsValid) { try { using (UoW) { clientDoc.InputBy = UserName; clientDoc.Document.DocumentID = clientDoc.DocumentID; //EF breaks without this hack... clientDoc.Document.LastModifiedBy = UserName; clientDoc.Document.LastModifiedDate = Now; UoW.Document.Update(clientDoc.Document); UoW.ClientDocuments.Update(clientDoc); result = UoW.Commit() > 0; } } catch (Exception e) { } } return Json(new { Success = result }); }