| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- /*******************************************************************************
- * 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
- {
- /// <summary>
- /// 附件上传
- /// </summary>
- public partial class F_CMN_0101 : FormBase
- {
- #region 成员变量
- //产品ID
- private int _goodsId;
- // 添加附件的列表
- private DataTable _attachmenTable = new DataTable("attachmen");
- //添加附件的实体类集合
- private List<GoodsAttachmentEntity> _attList = new List<GoodsAttachmentEntity>();
- //修改附件的实体类集合
- private List<GoodsAttachmentEntity> _updateAttList = new List<GoodsAttachmentEntity>();
- #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 事件
- /// <summary>
- /// 窗体加载事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 上传按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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 私有方法
- /// <summary>
- /// 根据ID获取产品附件
- /// </summary>
- /// <returns></returns>
- private DataSet GetOrderFixedAttachment()
- {
- DataSet dsAtt = SystemModuleProxy.Service.GetAttachmentByGoodsId(_goodsId);
- return dsAtt;
- }
- /// <summary>
- /// 将产品附件上传到服务器
- /// </summary>
- /// <param name="attachmenTable">本地附件列表</param>
- /// <returns>服务器附件列表</returns>
- 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;
- }
- }
- /// <summary>
- /// 保存按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- /// <summary>
- /// 取消按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- /// <summary>
- /// 单击单元格内容事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 单击单元格内容事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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);
- }
- }
- /// <summary>
- /// 绑定数据表大小
- /// </summary>
- 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
- }
- }
|