/// <summary> /// 附件上传 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpload_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.CheckFileExists = true; //检查文件是否存在 dlg.Multiselect = false; //是否允许多选,false表示单选 dlg.CheckPathExists = true; //只限制上传word和excel dlg.Filter = "Office Files|*.doc;*.docx;*.xls;*.xlsx"; if ((bool)dlg.ShowDialog()) { string attpath = dlg.FileName; string attname = dlg.SafeFileName; string xpsname = attname.Substring(0, attname.LastIndexOf(".")) + ".xps"; string xpspath = System.IO.Path.Combine(FileManageHelper.GetPath(), xpsname); try { if (OfficeConverter.IsValidOfficeFile(attpath)) { OfficeConverter.ConvertToXps(attpath, xpspath); UplodeAttToService(attpath); } } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage()); } } }
/// <summary> /// 删除文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, RoutedEventArgs e) { if (WorkFileItemsSource != null || WorkFileItemsSource.Count > 0) { List <WorkFileInfo> files = WorkFileItemsSource.Where(r => r.IsChecked == true).ToList(); if (files.Count == 0) { MessageBox.Show("请勾选要删除的文件"); return; } List <string> guids = files.Select(r => r.GUID).ToList(); string error; bool result = FileManageHelper.DeleteWorkFile(guids, out error); if (result) { MessageBox.Show("删除成功"); GetWorkFiles(); chkAll.IsChecked = false; } else { MessageBox.Show(error, "删除失败"); } } }
/// <summary> /// 保存操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, RoutedEventArgs e) { WorkFileInfo file = this.CurrentFile; if (file != null) { if (!FileValidate(file)) { return; } string error = string.Empty; bool result = FileManageHelper.SaveWorkFiles(file, out error); if (result) { this.Close(); if (file.Attachments != null || file.Attachments.Count > 0) { DeleteClientAtts(file.Attachments); } if (AfterSaveEvent != null) { AfterSaveEvent(CurrentFile); } } else { MessageBox.Show(error, "保存失败"); } } }
/// <summary> /// 删除目录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DelTreeItem_Click(object sender, RoutedEventArgs e) { TreeNode <CatalogInfo> selectnode = treeCatalog.SelectedItem as TreeNode <CatalogInfo>; if (selectnode != null) { if (selectnode.Level == 0) { MessageBox.Show("不可以删除根目录"); } else { List <string> guids = GetDeleteItemGuid(selectnode); string result = FileManageHelper.DeleteCatalog(guids); if (string.IsNullOrEmpty(result)) { InitCatalogTree(); //MessageBox.Show("删除成功"); CatalogInfoSources.Remove(selectnode); } else { MessageBox.Show(result, "删除失败"); } } } }
/// <summary> /// 添加目录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddTreeViewItem_Click(object sender, RoutedEventArgs e) { if (treeCatalog.SelectedItem != null) { TreeNode <CatalogInfo> selectnode = treeCatalog.SelectedItem as TreeNode <CatalogInfo>; if (selectnode.Level < 2) { TreeNode <CatalogInfo> newcatalog = new TreeNode <CatalogInfo>(); CatalogInfo catalog = new CatalogInfo(); catalog.Guid = Guid.NewGuid().ToString(); catalog.Activity_guid = activity.Guid; catalog.Parent_guid = selectnode.Value.Guid; catalog.Name = CreateCatalogName(); catalog.CreateDate = DateTime.Now; newcatalog.Value = catalog; string error = string.Empty; bool result = FileManageHelper.SaveCatalog(newcatalog.Value, out error); if (result) { selectnode.SubTreeNodes.Add(newcatalog); } else { MessageBox.Show(error, "目录添加失败"); } } else { MessageBox.Show("只可以创建三层目录"); } } }
////无限接循环子节点添加到根节点下面 //private void SetSubCatalog(List<CatalogInfo> catalogs, CatalogTreeNode catalognode) //{ // List<CatalogInfo> subcatalogs = catalogs.Where(c => c.Parent_guid == catalognode.Catalog.Guid).ToList(); // foreach (CatalogInfo catalog in subcatalogs) // { // CatalogTreeNode node = new CatalogTreeNode(); // node.Catalog = catalog; // SetSubCatalog(catalogs, node); // catalognode.Children.Add(node); // } //} #endregion #region 文件相关操作 /// <summary> /// 查询工作文件 /// </summary> /// <returns></returns> private void GetWorkFiles() { if (this.SelectedCatalogItem != null) { string catalogguid = this.SelectedCatalogItem.Value.Guid; List <WorkFileInfo> files = FileManageHelper.GetWorkFiles(this.activity.Guid, catalogguid); DefaultWorkFileItemsSource = files; WorkFileItemsSource = new ObservableCollection <WorkFileInfo>(files); } }
/// <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="file"></param> private void DeleteClientAtts(List <FileAttachment> attachments) { foreach (FileAttachment att in attachments) { string savePath = FileManageHelper.GetPath() + att.AttName; string xpsPath = savePath.Substring(0, savePath.LastIndexOf(".")).ToString() + ".xps"; if (System.IO.File.Exists(savePath)) { System.IO.File.Delete(savePath); System.IO.File.Delete(xpsPath); } } }
/// <summary> /// 重命名文本框失去焦点时,保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void renametextbox_LostFocus(object sender, RoutedEventArgs e) { //TextBox tempTextBox = AT_BC.Common.VisualTreeHelperExtension.GetChildObject<TextBox>(SelectTreeViewItem, "renametextbox"); //tempTextBox.Visibility = Visibility.Collapsed; TextBox txb = sender as TextBox; if (ValidName(txb.Text)) { txb.Visibility = Visibility.Collapsed; this.SelectedCatalogItem.Value.Name = txb.Text; string error = null; FileManageHelper.SaveCatalog(SelectedCatalogItem.Value, out error); } else { txb.Visibility = Visibility.Visible; } }
/// <summary> /// 下载文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DownloadFile_Click(object sender, RoutedEventArgs e) { try { FileAttachment attachment = (sender as System.Windows.Documents.Hyperlink).DataContext as FileAttachment; if (attachment == null) { return; } string docstr = attachment.AttName.Substring(attachment.AttName.LastIndexOf('.')); string serviceDocPath = FileManageHelper.ByteConvertDocService(attachment.AttContent, attachment.AttName); WebClient webClient = new WebClient(); SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = "文件下载"; //dlg.Reset(); if (docstr == ".doc" || docstr == ".docx") { dlg.Filter += "Word 文件|*.doc;*.docx"; } else if (docstr == ".xls" || docstr == ".xlsx") { dlg.Filter += "Excel 文件|*.xls;*.xlsx"; } // dlg.Filter = "Office Files|*.doc;*.docx;*.xls;*.xlsx"; if (dlg.ShowDialog() == true) { try { webClient.DownloadFile(serviceDocPath, dlg.FileName); MessageBox.Show("文件下载成功!"); } catch (Exception ec) { MessageBox.Show(ec.GetExceptionMessage()); } } } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage()); } }
private void dgList_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (SelectFileAttachment == null) { return; //防止单击空白或标题等触发该事件 } string xpspath = FileManageHelper.GetPath() + SelectFileAttachment.AttName.Substring(0, SelectFileAttachment.AttName.LastIndexOf(".")) + ".xps"; if (System.IO.File.Exists(xpspath)) { //已经存xps文件,直接加载 using (XpsDocument xpsDoc = new XpsDocument(xpspath, FileAccess.Read)) { var fsxps = xpsDoc.GetFixedDocumentSequence(); xpsDocViewr.Document = fsxps; } } else { string filepath = FileManageHelper.ByteConvertDocService(SelectFileAttachment.AttContent, SelectFileAttachment.AttName); string xpsfilepath = filepath.Substring(0, filepath.LastIndexOf(".")).ToString() + ".xps"; try { if (OfficeConverter.IsValidOfficeFile(filepath)) { OfficeConverter.ConvertToXps(filepath, xpsfilepath); using (XpsDocument xpsDoc = new XpsDocument(xpsfilepath, FileAccess.Read)) { var fsxps = xpsDoc.GetFixedDocumentSequence(); xpsDocViewr.Document = fsxps; } } } catch (Exception ex) { MessageBox.Show(ex.GetExceptionMessage()); } } }
/// <summary> /// 初始化目录树 /// </summary> /// <param name="activityguid"></param> private void InitCatalogTree() { CatalogInfoSources = new ObservableCollection <TreeNode <CatalogInfo> >(); string activityguid = CO_IA.Client.RiasPortal.ModuleContainer.Activity.Guid; List <CatalogInfo> catalogs = FileManageHelper.QueryCatalogs(activityguid); //存在目录信息 if (catalogs != null && catalogs.Count > 0) { ObservableCollection <TreeNode <CatalogInfo> > source = TreeNode <CatalogInfo> .CreateTreeNodes(catalogs, org => string.IsNullOrWhiteSpace(org.Parent_guid), (org, node) => { return(org.Parent_guid == node.Value.Guid); }); CatalogInfoSources = source; } //不存在目录信息 else { TreeNode <CatalogInfo> newnode = CreateRootTreeNode(); string error = string.Empty; bool result = FileManageHelper.SaveCatalog(newnode.Value, out error); if (result) { CatalogInfoSources.Add(newnode); } else { MessageBox.Show(error, "保存失败"); } } if (CatalogInfoSources != null && CatalogInfoSources.Count > 0) { CatalogInfoSources[0].IsSelected = true; } }
/// <summary> /// 初始加载附件列表 /// </summary> protected ObservableCollection <FileAttachment> QueryAttachments(string attguid) { return(FileManageHelper.QueryFileAttachments(attguid)); }