public static void ManageAttachment(List <AttachmentViewModel> attachmentList) { try { //User userInfo = UserService.GetSessionUserInfo(); using (TransactionScope scope = new TransactionScope()) { using (var context = new PYMFEEEntities()) { //Save attachment foreach (var attachmentViewModel in attachmentList) { var attachment = new Attachment(); MVMMappingService.MoveData(attachmentViewModel, attachment); if (attachmentViewModel.DeleteFlag) { if (attachment.ID != 0) { context.Entry(attachment).State = System.Data.Entity.EntityState.Deleted; } } else { if (attachment.ID != 0) { context.Entry(attachment).State = System.Data.Entity.EntityState.Modified; } else { attachment.AttachDate = DateTime.Now; attachment.AttachBy = ""; context.Attachments.Add(attachment); } } context.SaveChanges(); } } scope.Complete(); } //Move file AttachmentService.ManageFile(attachmentList); } catch (Exception ex) { } }
public static List <AttachmentViewModel> GetAttachmentList(AttachmentViewModel option) { List <AttachmentViewModel> attachmentViewModelList = new List <AttachmentViewModel>(); try { var documentTypeValueHelp = new List <ValueHelpViewModel>(); if (!string.IsNullOrEmpty(option.DocumentTypeValueType)) { documentTypeValueHelp = ValueHelpService.GetValueHelp(option.DocumentTypeValueType); } using (var context = new PYMFEEEntities()) { List <Attachment> attachmentList = new List <Attachment>(); //if(option.DataID != 0) //{ // attachmentList = (from m in context.Attachments where m.ProcessCode == option.ProcessCode && m.DataID == option.DataID select m).OrderBy(m => m.ID).ToList(); //} if (!string.IsNullOrEmpty(option.DataKey)) { attachmentList = (from m in context.Attachments where m.ProcessCode == option.ProcessCode && m.DataKey == option.DataKey select m).OrderBy(m => m.ID).ToList(); } foreach (var attachment in attachmentList) { if (!string.IsNullOrEmpty(option.AttachmentGroup)) { if (!string.Equals(attachment.AttachmentGroup, option.AttachmentGroup, StringComparison.OrdinalIgnoreCase)) { continue; } } AttachmentViewModel attachmentViewModel = new AttachmentViewModel(); MVMMappingService.MoveData(attachment, attachmentViewModel); //Download url UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext); string downloadUrl = helper.Action("DownloadFile", "Attachment", new { Area = "Shared" }); string downloadParam = option.DocumentFilePath + "/" + attachment.SavedFileName; //Encoding parameter var plainTextBytes = System.Text.Encoding.Unicode.GetBytes(downloadParam); var encodedParam = System.Convert.ToBase64String(plainTextBytes); downloadUrl = downloadUrl + "?downloadParam=" + encodedParam; attachmentViewModel.DownloadURL = downloadUrl; attachmentViewModel.DocumentTypeValueHelp = documentTypeValueHelp; attachmentViewModelList.Add(attachmentViewModel); } } } catch (Exception ex) { } return(attachmentViewModelList); }