Beispiel #1
0
        /// <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());
                }
            }
        }
Beispiel #2
0
        /// <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();//上传成功后,关闭流
                }
            }
        }
Beispiel #3
0
 /// <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);
         }
     }
 }
Beispiel #4
0
        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());
                }
            }
        }