/******************************************************************************* * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential * 类的信息: * 1.程序名称:F_CMN_0101.cs * 2.功能描述:附件上传 * 编辑履历: * 作者 日期 版本 修改内容 * 庄天威 2014/09/13 1.00 新建 *******************************************************************************/ using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Windows.Forms; using Dongke.IBOSS.PRD.Basics.BaseControls; using Dongke.IBOSS.PRD.Basics.BaseResources; using Dongke.IBOSS.PRD.Basics.Library; using Dongke.IBOSS.PRD.WCF.DataModels; using Dongke.IBOSS.PRD.WCF.Proxys; using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService; namespace Dongke.IBOSS.PRD.Client.CommonModule { /// /// 附件上传 /// public partial class F_CMN_0101 : FormBase { #region 成员变量 //产品ID private int _goodsId; // 添加附件的列表 private DataTable _attachmenTable = new DataTable("attachmen"); //添加附件的实体类集合 private List _attList = new List(); //修改附件的实体类集合 private List _updateAttList = new List(); #endregion #region 构造函数 public F_CMN_0101(int GoodsId) { InitializeComponent(); this._goodsId = GoodsId; this.Text = FormTitles.F_CMN_0101; this.btnUpload.Text = ButtonText.BTN_UPLOAD; this.btnSave.Text = ButtonText.BTN_SAVE; this.btnCancel.Text = ButtonText.BTN_CANCEL; } #endregion #region 事件 /// /// 窗体加载事件 /// /// /// private void F_MST_0704_Load(object sender, EventArgs e) { try { // 设置不自动创建列 this.dgvAttachmentNow.AutoGenerateColumns = false; // 加载已存在的附件 DataSet dsAtt = GetOrderFixedAttachment(); BindDGVSize(); this.dgvAttachmentNow.DataSource = dsAtt.Tables[0]; // 赋予成员变量附件列表_attachmenTable结构 DataColumn fileNameColumn = new DataColumn("FileName"); DataColumn localFilePathColumn = new DataColumn("LocalFilePath"); this._attachmenTable.Columns.Add(fileNameColumn); this._attachmenTable.Columns.Add(localFilePathColumn); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 上传按钮事件 /// /// /// private void btnUpload_Click(object sender, EventArgs e) { try { string fileName = string.Empty; // 打开选择文件对话框 odlgFile.Filter = Constant.FILTER_DOC; odlgFile.FilterIndex = 0; odlgFile.RestoreDirectory = true; odlgFile.Title = "选择附件"; odlgFile.FileName = null; odlgFile.RestoreDirectory = true; if (odlgFile.ShowDialog() == DialogResult.OK) { FileInfo file = new FileInfo(odlgFile.FileName); this.txtUploadFile.Text = odlgFile.FileName; // 将员工的附件添加到列表中 fileName = Path.GetFileName(this.txtUploadFile.Text.Trim()); this._attachmenTable.Rows.Add(fileName, this.txtUploadFile.Text); this.dgvAttachment.DataSource = this._attachmenTable; } BindDGVSize(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } #endregion #region 私有方法 /// /// 根据ID获取产品附件 /// /// private DataSet GetOrderFixedAttachment() { DataSet dsAtt = SystemModuleProxy.Service.GetAttachmentByGoodsId(_goodsId); return dsAtt; } /// /// 将产品附件上传到服务器 /// /// 本地附件列表 /// 服务器附件列表 private int UpLoadAttachmen(DataTable attachmenTable) { try { int MyReturn = 0; if ((attachmenTable != null && attachmenTable.Rows.Count > Constant.INT_IS_ZERO) || this._updateAttList.Count != Constant.INT_IS_ZERO) { // 循环员工的附件上传到服务器并保存 foreach (DataRow dataRow in attachmenTable.Rows) { byte[] fileBinary = Utility.GetBytesByFilePath(dataRow["LocalFilePath"].ToString()); String FileName = dataRow["FileName"].ToString(); String FileType = FileName.Substring(FileName.LastIndexOf(".")); string filePath = CommonModuleProxy.Service.UpLoadFile("Goods", DateTime.Now, FileType, fileBinary); // 保存实体 GoodsAttachmentEntity attEntity = new GoodsAttachmentEntity(); attEntity.FileName = FileName; attEntity.FilePath = filePath; attEntity.GoodsID = this._goodsId; attEntity.IsUpdateAdd = 1; this._attList.Add(attEntity); } if (this._updateAttList.Count != Constant.INT_IS_ZERO) { foreach (GoodsAttachmentEntity attFor in this._updateAttList) { this._attList.Add(attFor); } MyReturn = SystemModuleProxy.Service.UpdateAttachment(this._attList, this._goodsId); } else { MyReturn = SystemModuleProxy.Service.AddAttachment(this._attList, this._goodsId); } } return MyReturn; } catch (Exception ex) { throw ex; } } /// /// 保存按钮事件 /// /// /// private void btnSave_Click(object sender, EventArgs e) { try { int RowsCount = UpLoadAttachmen(this._attachmenTable); if (RowsCount > Constant.INT_IS_ZERO) { this._attachmenTable.Clear(); this._attList.Clear(); this._updateAttList.Clear(); DataSet dsAtt = GetOrderFixedAttachment(); BindDGVSize(); this.dgvAttachmentNow.DataSource = dsAtt.Tables[0]; this.txtUploadFile.Text = ""; MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "附件", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "附件", "保存"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { throw ex; } } /// /// 取消按钮事件 /// /// /// private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } /// /// 单击单元格内容事件 /// /// /// private void dgvAttachmentNow_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { if (this.dgvAttachmentNow.CurrentRow != null) { // 点击下载 if (e.ColumnIndex == this.dgvAttachmentNow.CurrentRow.Cells["fileDownLoad"].ColumnIndex) { // 获取附件的保存路径 string fileSavePath = string.Empty; sdlgFile.Filter = Constant.FILTER_DOC; sdlgFile.FilterIndex = 0; sdlgFile.RestoreDirectory = true; sdlgFile.Title = "保存附件"; sdlgFile.FileName = this.dgvAttachmentNow.CurrentRow.Cells["FileName"].Value + ""; sdlgFile.AddExtension = true; if (sdlgFile.ShowDialog() == DialogResult.OK) { fileSavePath = sdlgFile.FileName; } else { return; } // 下载附件 string downLoadPath = this.dgvAttachmentNow.CurrentRow.Cells["FilePath"].Value.ToString(); byte[] downLoadFile = CommonModuleProxy.Service.DownloadFile(downLoadPath); // 若获取的文件流为null,提示文件不存在 if (downLoadFile != null) { // 保存附件 bool result = Utility.BinaryToFile(fileSavePath, downLoadFile); // 提示保存结果 if (result == true) { MessageBox.Show("附件下载成功。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information); } else { MessageBox.Show("附件下载失败。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning); } } else { MessageBox.Show("该附件文件不存在。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning); } } // 点击删除 if (e.ColumnIndex == this.dgvAttachmentNow.CurrentRow.Cells["deleteFile"].ColumnIndex) { if (this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value != null && this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value != DBNull.Value) { GoodsAttachmentEntity goodsAttEntity = new GoodsAttachmentEntity(); goodsAttEntity.AttachmentID = Convert.ToInt32(this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value); goodsAttEntity.ValueFlag = 0; goodsAttEntity.IsUpdateAdd = 0; this._updateAttList.Add(goodsAttEntity); } dgvAttachmentNow.Rows.RemoveAt(dgvAttachmentNow.CurrentRow.Index); } } } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 单击单元格内容事件 /// /// /// private void dgvAttachment_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == this.dgvAttachment.CurrentRow.Cells["fileDeleteNew"].ColumnIndex) { dgvAttachment.Rows.RemoveAt(dgvAttachment.CurrentRow.Index); } BindDGVSize(); } catch (Exception ex) { // 对异常进行共通处理 ExceptionManager.HandleEventException(this.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex); } } /// /// 绑定数据表大小 /// private void BindDGVSize() { if (this.dgvAttachment.Rows.Count == 0) { this.gbxNews.Visible = false; this.gbxNow.Height = 370; } else { this.gbxNews.Visible = true; this.gbxNow.Height = 185; } } #endregion } }