/// <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()); } } }