/// <summary> /// 附件列表,单选 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void chkCell_Click(object sender, RoutedEventArgs e) { CheckBox box = sender as CheckBox; bool? isChecked = box.IsChecked; if (!isChecked.HasValue) { return; } FileAttachment att = box.DataContext as FileAttachment; bool checkedState = isChecked.Value; att.IsChecked = isChecked.Value; int ischeckCount = AttachmentSources.Count(p => p.IsChecked == true); if (ischeckCount == AttachmentSources.Count) { chkAll.IsChecked = true; } else if (ischeckCount == 0) { chkAll.IsChecked = false; } else { this.chkAll.IsChecked = null; } }
/// <summary> /// 上传到服务器指定目录 /// </summary> /// <param name="ServerPath"></param> /// <param name="EnclouseFolderPath"></param> /// <returns></returns> private void UplodeAttToService(string ServerPath) { //获取文件名 string fileName = ServerPath.Substring(ServerPath.LastIndexOf("\\") + 1); string filePath = System.IO.Path.Combine(FileManageHelper.GetPath(), fileName); FileStream fs = null; try { WebClient web = new WebClient(); web.Credentials = CredentialCache.DefaultCredentials; //初始化上传文件 打开读取 fs = new FileStream(ServerPath, FileMode.Open, FileAccess.Read); if (fs.Length / 1024 / 1024 > 20) { MessageBox.Show("上传附件不支持超过20M大小的文件。"); } else { BinaryReader br = new BinaryReader(fs); byte[] btArray = br.ReadBytes((int)fs.Length); Stream uplodeStream = web.OpenWrite(filePath, "PUT"); if (uplodeStream.CanWrite) { uplodeStream.Write(btArray, 0, btArray.Length); uplodeStream.Flush(); uplodeStream.Close(); FileAttachment att = new FileAttachment(); att.DataState = DataStateEnum.Added; att.GUID = System.Guid.NewGuid().ToString(); att.FileGuid = CurrentFile.GUID; att.AttName = fileName; att.AttSize = (Int32)fs.Length; att.AttContent = btArray; att.UploadData = DateTime.Now.Date; AttachmentSources.Add(att); UpdateAttSource(); } } } catch (Exception ex) { throw ex; } finally { if (fs != null) { fs.Close();//上传成功后,关闭流 } } }
/// <summary> /// 附件列表删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_delete_Click(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("确认要删除选中项?", "提示", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { if (AttachmentSources != null || AttachmentSources.Count > 0) { List <FileAttachment> delatt = AttachmentSources.Where(p => p.IsChecked == true).ToList(); foreach (FileAttachment att in delatt) { att.DataState = DataStateEnum.Deleted; } UpdateAttSource(); chkAll.IsChecked = false; } if (AttachmentSources == null || AttachmentSources.Count == 0) { xpsDocViewr.Document = null; } } }
/// <summary> /// 更新附件数据源 /// </summary> private void UpdateAttSource() { CurrentFile.Attachments = new List <FileAttachment>(AttachmentSources); dgList.ItemsSource = AttachmentSources.Where(r => r.DataState != DataStateEnum.Deleted); }