public JsonResult AddVendorDocument(VendorDocument vendorDoc, Document file) { var result = false; DateTime Now = DateTime.Now; string UserName = System.Web.HttpContext.Current.User.Identity.Name; string newDir = Server.MapPath("~/Storage/VendorDocument/"); //if (ModelState.IsValid) //{ if (file.DocumentID == Guid.Empty) //-- 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 Vendor-Document Table //====================================================================== //var vendorDocument = new VendorDocument(); if (vendorDoc.VendorDocumentID == Guid.Empty) { vendorDoc.VendorDocumentID = Guid.NewGuid(); vendorDoc.DocumentID = file.DocumentID; //vendorDocument.VendorID = vendorDoc.VendorID; vendorDoc.InputBy = UserName; vendorDoc.InputDate = Now; this.UoW.VendorDocument.Insert(vendorDoc); } //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; } } } return Json(new { Success = result }); //} //else //{ // return Json(new { Success = result, Message = "Invalid Model" }); //} }
public JsonResult EditVendorDocument(VendorDocument vendorDoc) { bool result = false; if (ModelState.IsValid) { try { using (UoW) { vendorDoc.Document.DocumentID = vendorDoc.DocumentID; //EF breaks without this hack... UoW.VendorDocument.Update(vendorDoc); result = UoW.Commit() > 0; } } catch (Exception e) { } } return Json(new { Success = result }); }