SheetAttachment.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:19k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Design;
- using System.Collections.Generic;
- [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
- namespace OThinker.H3.WorkSheet
- {
- /// <summary>
- /// 用于管理多附件,格式应该是
- /// 添加附件(可写的时候可见) 读取附件1的链接 删除附件1的链接(可写的时候可见) 读取附件2的链接 删除附件2的链接 (可写的时候可见)
- /// </summary>
- [DefaultProperty("Text"),
- ToolboxData("<{0}:SheetAttachment runat=server></{0}:SheetAttachment>")]
- public class SheetAttachment : System.Web.UI.WebControls.WebControl, ISheetControl
- {
- public SheetAttachment()
- {
- this.ListBox = new ListBox();
- this.Controls.Add(this.ListBox);
- this.HiddenField = new HiddenField();
- this.Controls.Add(this.HiddenField);
- this.FileUpload = new FileUpload();
- this.Controls.Add(this.FileUpload);
- this.RemovedFiles = new TextBox();
- this.RemovedFiles.Attributes.Add("style", "display: none;");
- this.Controls.Add(this.RemovedFiles);
- }
- private ListBox ListBox;
- private HiddenField HiddenField;
- private FileUpload FileUpload;
- private TextBox RemovedFiles;
- private bool _Enabled = true;
- public override bool Enabled
- {
- get
- {
- return this._Enabled;
- }
- set
- {
- this._Enabled = value;
- }
- }
- #region DataField属性
- private string _DataField;
- [Bindable(true), Category("WorkSheet"), DefaultValue("")]
- [Editor(typeof(DataSelector), typeof(UITypeEditor))]
- public string DataField
- {
- get
- {
- return this._DataField;
- }
- set
- {
- this._DataField = value;
- }
- }
- #endregion
- #region ISheetControl 接口
- /// <summary>
- /// 加载数据
- /// </summary>
- /// <param name="SheetPage"></param>
- public virtual void LoadDataField(OThinker.H3.WorkSheet.SheetPage SheetPage)
- {
- if (this.DataField == null || this.DataField == "")
- {
- // 数据字段为空
- }
- else if (SheetPage.Enviroment.InstanceData[this.DataField] == null)
- {
- // 检查是否存在
- }
- else if (!OThinker.H3.Data.DataLogicTypeConvertor.IsAttachmentType(SheetPage.Enviroment.InstanceData[this.DataField].LogicType))
- {
- // 类型不匹配
- }
- else if (SheetPage.Enviroment.InstanceData[this.DataField].Visible == false)
- {
- // 不能见
- this.Visible = false;
- }
- else
- {
- // 如果能够修改则允许上传
- this.Enabled = SheetPage.Enviroment.InstanceData[this.DataField].Editable;
- }
- }
- /// <summary>
- /// 验证数据
- /// </summary>
- /// <param name="SheetPage"></param>
- /// <param name="Errors"></param>
- /// <returns></returns>
- public virtual bool ValidateDataField(
- OThinker.H3.WorkSheet.SheetPage SheetPage,
- System.Collections.Generic.List<string> Errors)
- {
- return true;
- }
- /// <summary>
- /// 保存数据
- /// </summary>
- /// <param name="SheetPage"></param>
- public virtual void SaveDataField(OThinker.H3.WorkSheet.SheetPage SheetPage)
- {
- // 将要删除的文件删除
- string removedFileText = this.RemovedFiles.Text;
- string[] removedFiles = null;
- if (removedFileText != null)
- {
- removedFiles = removedFileText.Split(';');
- }
- if (removedFiles != null)
- {
- foreach (string id in removedFiles)
- {
- if (id != null && id != "")
- {
- OThinker.H3.Server.Engine.InstanceDataManager.RemoveAttachment(id);
- }
- }
- }
- // 添加新添加的文件
- foreach (string key in SheetPage.Request.Files.AllKeys)
- {
- if (key == null || key == "")
- {
- continue;
- }
- if (key.IndexOf(this.ClientID) != 0)
- {
- continue;
- }
- System.Web.HttpPostedFile file = SheetPage.Request.Files[key];
- if (file == null || file.FileName == null || file.FileName == "")
- {
- continue;
- }
- // 添加这个附件
- OThinker.H3.Data.Attachment attachment = new OThinker.H3.Data.Attachment();
- attachment.Activity = SheetPage.Enviroment.ActivityName;
- byte[] content = new byte[file.InputStream.Length];
- file.InputStream.Read(content, 0, content.Length);
- attachment.Content = content;
- attachment.ContentType = file.ContentType;
- attachment.CreatedBy = SheetPage.Enviroment.Participant;
- attachment.CreatedTime = System.DateTime.Now;
- attachment.DataField = this.DataField;
- attachment.Description = null;
- attachment.TokenId = SheetPage.Enviroment.WorkItem.TokenId;
- attachment.FileName = file.FileName.Substring(file.FileName.LastIndexOf('\') + 1);
- attachment.InstanceId = SheetPage.Enviroment.InstanceId;
- attachment.LastVersion = true;
- attachment.ModifiedBy = null;
- attachment.ModifyTime = System.DateTime.Now;
- OThinker.H3.Server.Engine.InstanceDataManager.AddAttachment(attachment);
- }
- }
- #endregion
- protected override void Render(HtmlTextWriter output)
- {
- // 显示读取的模块
- if (this.DesignMode)
- {
- for (int count = 0; count < 3; count++)
- {
- HyperLink lnkAttachment = new HyperLink();
- lnkAttachment.NavigateUrl = "";
- lnkAttachment.Target = "_blank";
- lnkAttachment.Text = "文件" + count;
- lnkAttachment.RenderControl(output);
- HyperLink lnkRemove = new HyperLink();
- lnkRemove.Text = "删除";
- lnkAttachment.NavigateUrl = "";
- lnkRemove.RenderControl(output);
- Label separator = new Label();
- separator.Text = " ";
- separator.RenderControl(output);
- }
- }
- else
- {
- // 查询
- OThinker.H3.Data.AttachmentHeader[] headers = null;
- if (((SheetPage)this.Page).Enviroment.InstanceId == Instance.InstanceContext.UnspecifiedID)
- {
- // 流程的创建状态
- headers = null;
- }
- else
- {
- // 读取已经上传的文件
- headers = OThinker.H3.Server.Engine.InstanceDataManager.QueryAttachment(((SheetPage)this.Page).Enviroment.InstanceId, this.DataField, OThinker.Data.BoolMatchValue.True, null);
- }
- if (headers == null || headers.Length == 0)
- {
- output.Write("没有附件");
- }
- else
- {
- foreach (OThinker.H3.Data.AttachmentHeader header in headers)
- {
- if (header == null)
- {
- continue;
- }
- HyperLink lnkAttachment = new HyperLink();
- this.Controls.Add(lnkAttachment);
- lnkAttachment.NavigateUrl = ((SheetPage)this.Page).Enviroment.GetReadAttachment(header.AttachmentID);
- lnkAttachment.Target = "_blank";
- lnkAttachment.Text = header.FileName;
- // 如果没有这句话,则不会产生ID
- lnkAttachment.Attributes.Add(
- "onclick",
- "javascript:" +
- "document.getElementById('" + this.RemovedFiles.ClientID + "');" +
- "document.getElementById('" + lnkAttachment.ClientID + "');");
- lnkAttachment.RenderControl(output);
- HyperLink lnkRemove = new HyperLink();
- this.Controls.Add(lnkRemove);
- lnkRemove.ImageUrl = ((SheetPage)this.Page).Enviroment.GetDeleteAttachmentImageUrl();
- lnkRemove.Attributes.Add("style", "cursor: hand");
- lnkRemove.Attributes.Add(
- "onclick",
- "javascript:" +
- "document.getElementById('" + this.RemovedFiles.ClientID + "').value += ('" + header.AttachmentID + ";');" +
- "document.getElementById('" + lnkAttachment.ClientID + "').style.display = 'none';" +
- "document.getElementById('" + lnkRemove.ClientID + "').style.display = 'none';");
- if (this.Enabled)
- {
- lnkRemove.RenderControl(output);
- }
- Label separator = new Label();
- separator.Text = " ";
- separator.RenderControl(output);
- }
- }
- }
- // 记录删除的附件的控件
- if (!this.DesignMode)
- {
- this.RemovedFiles.RenderControl(output);
- }
- // 显示添加附件模块
- if (this.Enabled)
- {
- this.ListBox.Attributes.Add("style", "width:80%;");
- string divId = this.ClientID + "_DivAppend";
- string hideButton = this.ClientID + "_HideButton";
- string displayButton = this.ClientID + "_DisplayButton";
- // 隐藏显示按钮
- output.Write("<input type="button" name="" + hideButton + "" value="-" style="display: none;" onClick="javascript:" + divId + ".style.display='none';" + hideButton + ".style.display='none';" + displayButton + ".style.display='block';">");
- output.Write("<input type="button" name="" + displayButton + "" value="+" onClick="javascript:" + divId + ".style.display='block';" + hideButton + ".style.display='block';" + displayButton + ".style.display='none';">");
- output.Write("<div id="" + divId + "" style="display: none;">");
- string addinput = this.ClientID + "_addinput";
- string fileNum = this.ClientID + "_fileNum";
- string remove = this.ClientID + "_remove";
- string file = this.ClientID + "_file";
- string files = this.ClientID + "_files";
- if (!this.Page.ClientScript.IsClientScriptBlockRegistered("SheetMultiAttachmentScript"))
- {
- output.Write("<script type="text/javascript">" + "rn");
- output.Write("var " + fileNum + " = 0;" + "rn");
- // 添加
- output.Write("function " + addinput + "()" + "rn");
- output.Write("{" + "rn");
- output.Write("//隐藏File控件" + "rn");
- output.Write("var ofile = document.getElementById("" + file + "" + " + fileNum + ");" + "rn");
- output.Write("if(ofile.value=='')" + "rn");
- output.Write("{" + "rn");
- output.Write("return;" + "rn");
- output.Write("}" + "rn");
- output.Write("// 检查是否重复" + "rn");
- output.Write("var myOptions=document.getElementById("" + this.ListBox.ClientID + "").options;" + "rn");
- output.Write("for (i=0; i<myOptions.length; i++)" + "rn");
- output.Write("{" + "rn");
- output.Write("if (myOptions[i].text == ofile.value)" + "rn");
- output.Write("{" + "rn");
- output.Write("alert("该文件已存在列表当中,请不要重复选择!");" + "rn");
- output.Write("return;" + "rn");
- output.Write("}" + "rn");
- output.Write("}" + "rn");
- output.Write("ofile.style.display = 'none';" + "rn");
- output.Write("//创建新file控件" + "rn");
- output.Write("var f = document.createElement("input");" + "rn");
- output.Write("" + fileNum + " = " + fileNum + " + 1;" + "rn");
- output.Write("f.type = 'file';" + "rn");
- output.Write("f.name = '" + file + "'+" + fileNum + ";" + "rn");
- output.Write("f.id = '" + file + "'+" + fileNum + ";" + "rn");
- output.Write("f.size = '20';" + "rn");
- output.Write("f.className = 'in3';" + "rn");
- output.Write("var div1 = document.getElementById("" + files + "")" + "rn");
- output.Write("div1.insertAdjacentElement("BeforeBegin", f);" + "rn");
- output.Write("// 添加到列表中" + "rn");
- output.Write("var mySelect = document.getElementById("" + this.ListBox.ClientID + "");" + "rn");
- output.Write("var option = new Option(ofile.value, ofile.name);" + "rn");
- output.Write("mySelect.options.add(option);" + "rn");
- output.Write("mySelect.options.selectedIndex = mySelect.options.length-1;" + "rn");
- // output.Write("mySelect.size++;" + "rn");
- output.Write("}" + "rn");
- output.Write("function " + remove + "()" + "rn");
- output.Write("{" + "rn");
- output.Write("var mySelect=document.getElementById("" + this.ListBox.ClientID + "");" + "rn");
- output.Write("var selectedIndex=mySelect.selectedIndex;" + "rn");
- output.Write("if (selectedIndex == -1)" + "rn");
- output.Write("{" + "rn");
- output.Write("alert("请选择要移除的文件!");" + "rn");
- output.Write("return;" + "rn");
- output.Write("}" + "rn");
- output.Write("var inputID = mySelect.options[selectedIndex].value;" + "rn");
- output.Write("mySelect.options.remove(selectedIndex);" + "rn");
- output.Write("if(selectedIndex == 0)" + "rn");
- output.Write("{" + "rn");
- output.Write("mySelect.options.selectedIndex=0;" + "rn");
- output.Write("}" + "rn");
- output.Write("else" + "rn");
- output.Write("{" + "rn");
- output.Write("mySelect.options.selectedIndex=selectedIndex-1;" + "rn");
- output.Write("}" + "rn");
- // output.Write("mySelect.size--;" + "rn");
- output.Write("//非电子文件,移出input按钮" + "rn");
- output.Write("if (inputID.lastIndexOf("\\")==-1)" + "rn");
- output.Write("{" + "rn");
- output.Write("var objinput = document.getElementById(inputID);" + "rn");
- output.Write("objinput.removeNode(true);" + "rn");
- output.Write("}" + "rn");
- output.Write("else" + "rn");
- output.Write("{" + "rn");
- output.Write("//删除隐藏域的一个值" + "rn");
- output.Write("var obj= document.getElementById("" + this.HiddenField.ClientID + "");" + "rn");
- output.Write("var str=obj.value;" + "rn");
- output.Write("var Files =new Array();" + "rn");
- output.Write("Files=str.split('|');" + "rn");
- output.Write("for (var i=0; i<Files.length;i++)" + "rn");
- output.Write("{" + "rn");
- output.Write("if (inputID == Files[i])" + "rn");
- output.Write("{" + "rn");
- output.Write("str=str.replace(inputID,"");" + "rn");
- output.Write("obj.value=str;" + "rn");
- output.Write("return;" + "rn");
- output.Write("}" + "rn");
- output.Write("}" + "rn");
- output.Write("}" + "rn");
- output.Write("}" + "rn");
- output.Write("</script>" + "rn");
- this.Page.ClientScript.RegisterClientScriptBlock(typeof(SheetAttachment), "SheetMultiAttachmentScript", "");
- }
- output.Write("<table width="100%" border="0">" + "rn");
- output.Write("<tr>" + "rn");
- output.Write("<td>" + "rn");
- output.Write("<table width="100%">" + "rn");
- output.Write("<tr>" + "rn");
- output.Write("<td>" + "rn");
- this.ListBox.RenderControl(output);
- output.Write("<input id="Button3" type="button" class="btn" value="删除" onclick="" + remove + "();"/>" + "rn");
- this.HiddenField.RenderControl(output);
- output.Write("</td>" + "rn");
- output.Write("</tr>" + "rn");
- output.Write("<tr>" + "rn");
- output.Write("<td>" + "rn");
- output.Write("<div id="" + files + "">" + "rn");
- this.FileUpload.ID = file + "0";
- this.FileUpload.RenderControl(output);
- output.Write("</div>" + "rn");
- output.Write("<input id="Button2" class="btn" type="button" value="添加文件" onclick="" + addinput + "();" />" + "rn");
- output.Write("</td>" + "rn");
- output.Write("</tr>" + "rn");
- output.Write("</table>" + "rn");
- output.Write("</td>" + "rn");
- output.Write("</tr>" + "rn");
- output.Write("</table>" + "rn");
- output.Write("</div>");
- }
- }
- }
- }