public static void InsertFichier(CRMFichier fic) { using (UpsilabEntities db = new UpsilabEntities()) { db.CRMFichier.Attach(fic); db.ObjectStateManager.ChangeObjectState(fic, System.Data.EntityState.Added); db.SaveChanges(); } }
public ActionResult InsertNote(CRMNote _crmNote, IEnumerable<HttpPostedFileBase> files) { List<FirmInstitution> lstFirmInstitutions = GetData(); try { CRMNote insertedNote = CRMNoteBL.InsertNote(_crmNote); CustomerProspect customer = CustomerProspectBL.GetById(_crmNote.idClient.Value); if (insertedNote != null && customer != null && files != null) { int i = 1; foreach (var file in files) { string sDocTypePrefix = ElectronicSafeDocumentBL.DocumentType.CRMNote.ToString(); DocumentType objDoctype = DocumentTypeBL.GetDocumentTypeByCode(sDocTypePrefix); if (file != null) { //Check file contenttype //if (FileManager.IsPDF(file.ContentType)) if (FileManager.IsPDF(file.FileName)) { string fullPath = ElectronicSafeDocumentBL.BuildClientUploadDocumentPath(sDocTypePrefix, customer); string fileName = ElectronicSafeDocumentBL.BuildDocumentName(string.Format(sDocTypePrefix + "_{0}_", i), customer.idCustomer, "CRMNote_{0}.pdf"); // 1- Sauvegarde dans le repertoire string filePath = System.IO.Path.Combine(fullPath, fileName); file.SaveAs(filePath); CRMFichier newFichier = new CRMFichier() { idCRMNote = insertedNote.idCRMNote, nom = fileName, chemin = filePath }; CRMFichierBL.InsertFichier(newFichier); } else { ViewBag.Style = "style='color:red'"; ViewBag.Message = ViewBag.Message + LanguageData.GetContent("fichier_pdf_requis"); return View("Index", GetClients(null, lstFirmInstitutions)); } i++; } } ViewBag.Style = "style=color:green"; ViewBag.Message = LanguageData.GetContent("note_bien_enregistee"); ViewBag.Message = ViewBag.Message + "<br>" + string.Format(LanguageData.GetContent("crm_fichier_enregistement_reussi"), i - 1); } } catch (Exception ex) { ViewBag.Style = "style=color:red"; ViewBag.Message =LanguageData.GetContent("exception_titre") + ": " + ex.Message; } return View("Index", GetClients(null, lstFirmInstitutions)); }