SheetAttachment.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:19k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Design;
  7. using System.Collections.Generic;
  8. [assembly: TagPrefix("OThinker.H3.WorkSheet", "SheetControls")]
  9. namespace OThinker.H3.WorkSheet
  10. {
  11.     /// <summary>
  12.     /// 用于管理多附件,格式应该是
  13.     /// 添加附件(可写的时候可见) 读取附件1的链接 删除附件1的链接(可写的时候可见) 读取附件2的链接 删除附件2的链接 (可写的时候可见)
  14.     /// </summary>
  15.     [DefaultProperty("Text"),
  16.     ToolboxData("<{0}:SheetAttachment runat=server></{0}:SheetAttachment>")]
  17.     public class SheetAttachment : System.Web.UI.WebControls.WebControl, ISheetControl
  18.     {
  19.         public SheetAttachment()
  20.         {
  21.             this.ListBox = new ListBox();
  22.             this.Controls.Add(this.ListBox);
  23.             this.HiddenField = new HiddenField();
  24.             this.Controls.Add(this.HiddenField);
  25.             this.FileUpload = new FileUpload();
  26.             this.Controls.Add(this.FileUpload);
  27.             this.RemovedFiles = new TextBox();
  28.             this.RemovedFiles.Attributes.Add("style", "display: none;");
  29.             this.Controls.Add(this.RemovedFiles);
  30.         }
  31.         private ListBox ListBox;
  32.         private HiddenField HiddenField;
  33.         private FileUpload FileUpload;
  34.         private TextBox RemovedFiles;
  35.         private bool _Enabled = true;
  36.         public override bool Enabled
  37.         {
  38.             get
  39.             {
  40.                 return this._Enabled;
  41.             }
  42.             set
  43.             {
  44.                 this._Enabled = value;
  45.             }
  46.         }
  47.         #region DataField属性
  48.         private string _DataField;
  49.         [Bindable(true), Category("WorkSheet"), DefaultValue("")]
  50.         [Editor(typeof(DataSelector), typeof(UITypeEditor))]
  51.         public string DataField
  52.         {
  53.             get
  54.             {
  55.                 return this._DataField;
  56.             }
  57.             set
  58.             {
  59.                 this._DataField = value;
  60.             }
  61.         }
  62.         #endregion
  63.         #region ISheetControl 接口
  64.         /// <summary>
  65.         /// 加载数据
  66.         /// </summary>
  67.         /// <param name="SheetPage"></param>
  68.         public virtual void LoadDataField(OThinker.H3.WorkSheet.SheetPage SheetPage)
  69.         {
  70.             if (this.DataField == null || this.DataField == "")
  71.             {
  72.                 // 数据字段为空
  73.             }
  74.             else if (SheetPage.Enviroment.InstanceData[this.DataField] == null)
  75.             {
  76.                 // 检查是否存在
  77.             }
  78.             else if (!OThinker.H3.Data.DataLogicTypeConvertor.IsAttachmentType(SheetPage.Enviroment.InstanceData[this.DataField].LogicType))
  79.             {
  80.                 // 类型不匹配
  81.             }
  82.             else if (SheetPage.Enviroment.InstanceData[this.DataField].Visible == false)
  83.             {
  84.                 // 不能见
  85.                 this.Visible = false;
  86.             }
  87.             else
  88.             {
  89.                 // 如果能够修改则允许上传
  90.                 this.Enabled = SheetPage.Enviroment.InstanceData[this.DataField].Editable;
  91.             }
  92.         }
  93.         /// <summary>
  94.         /// 验证数据
  95.         /// </summary>
  96.         /// <param name="SheetPage"></param>
  97.         /// <param name="Errors"></param>
  98.         /// <returns></returns>
  99.         public virtual bool ValidateDataField(
  100.             OThinker.H3.WorkSheet.SheetPage SheetPage,
  101.             System.Collections.Generic.List<string> Errors)
  102.         {
  103.             return true;
  104.         }
  105.         /// <summary>
  106.         /// 保存数据
  107.         /// </summary>
  108.         /// <param name="SheetPage"></param>
  109.         public virtual void SaveDataField(OThinker.H3.WorkSheet.SheetPage SheetPage)
  110.         {
  111.             // 将要删除的文件删除
  112.             string removedFileText = this.RemovedFiles.Text;
  113.             string[] removedFiles = null;
  114.             if (removedFileText != null)
  115.             {
  116.                 removedFiles = removedFileText.Split(';');
  117.             }
  118.             if (removedFiles != null)
  119.             {
  120.                 foreach (string id in removedFiles)
  121.                 {
  122.                     if (id != null && id != "")
  123.                     {
  124.                         OThinker.H3.Server.Engine.InstanceDataManager.RemoveAttachment(id);
  125.                     }
  126.                 }
  127.             }
  128.             // 添加新添加的文件
  129.             foreach (string key in SheetPage.Request.Files.AllKeys)
  130.             {
  131.                 if (key == null || key == "")
  132.                 {
  133.                     continue;
  134.                 }
  135.                 if (key.IndexOf(this.ClientID) != 0)
  136.                 {
  137.                     continue;
  138.                 }
  139.                 System.Web.HttpPostedFile file = SheetPage.Request.Files[key];
  140.                 if (file == null || file.FileName == null || file.FileName == "")
  141.                 {
  142.                     continue;
  143.                 }
  144.                 // 添加这个附件
  145.                 OThinker.H3.Data.Attachment attachment = new OThinker.H3.Data.Attachment();
  146.                 attachment.Activity = SheetPage.Enviroment.ActivityName;
  147.                 byte[] content = new byte[file.InputStream.Length];
  148.                 file.InputStream.Read(content, 0, content.Length);
  149.                 attachment.Content = content;
  150.                 attachment.ContentType = file.ContentType;
  151.                 attachment.CreatedBy = SheetPage.Enviroment.Participant;
  152.                 attachment.CreatedTime = System.DateTime.Now;
  153.                 attachment.DataField = this.DataField;
  154.                 attachment.Description = null;
  155.                 attachment.TokenId = SheetPage.Enviroment.WorkItem.TokenId;
  156.                 attachment.FileName = file.FileName.Substring(file.FileName.LastIndexOf('\') + 1);
  157.                 attachment.InstanceId = SheetPage.Enviroment.InstanceId;
  158.                 attachment.LastVersion = true;
  159.                 attachment.ModifiedBy = null;
  160.                 attachment.ModifyTime = System.DateTime.Now;
  161.                 OThinker.H3.Server.Engine.InstanceDataManager.AddAttachment(attachment);
  162.             }
  163.         }
  164.         #endregion
  165.         protected override void Render(HtmlTextWriter output)
  166.         {
  167.             // 显示读取的模块
  168.             if (this.DesignMode)
  169.             {
  170.                 for (int count = 0; count < 3; count++)
  171.                 {
  172.                     HyperLink lnkAttachment = new HyperLink();
  173.                     lnkAttachment.NavigateUrl = "";
  174.                     lnkAttachment.Target = "_blank";
  175.                     lnkAttachment.Text = "文件" + count;
  176.                     lnkAttachment.RenderControl(output);
  177.                     HyperLink lnkRemove = new HyperLink();
  178.                     lnkRemove.Text = "删除";
  179.                     lnkAttachment.NavigateUrl = "";
  180.                     lnkRemove.RenderControl(output);
  181.                     Label separator = new Label();
  182.                     separator.Text = "  ";
  183.                     separator.RenderControl(output);
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 // 查询
  189.                 OThinker.H3.Data.AttachmentHeader[] headers = null;
  190.                 if (((SheetPage)this.Page).Enviroment.InstanceId == Instance.InstanceContext.UnspecifiedID)
  191.                 {
  192.                     // 流程的创建状态
  193.                     headers = null;
  194.                 }
  195.                 else
  196.                 {
  197.                     // 读取已经上传的文件
  198.                     headers = OThinker.H3.Server.Engine.InstanceDataManager.QueryAttachment(((SheetPage)this.Page).Enviroment.InstanceId, this.DataField, OThinker.Data.BoolMatchValue.True, null);
  199.                 }
  200.                 if (headers == null || headers.Length == 0)
  201.                 {
  202.                     output.Write("没有附件");
  203.                 }
  204.                 else
  205.                 {
  206.                     foreach (OThinker.H3.Data.AttachmentHeader header in headers)
  207.                     {
  208.                         if (header == null)
  209.                         {
  210.                             continue;
  211.                         }
  212.                         HyperLink lnkAttachment = new HyperLink();
  213.                         this.Controls.Add(lnkAttachment);
  214.                         lnkAttachment.NavigateUrl = ((SheetPage)this.Page).Enviroment.GetReadAttachment(header.AttachmentID);
  215.                         lnkAttachment.Target = "_blank";
  216.                         lnkAttachment.Text = header.FileName;
  217.                         // 如果没有这句话,则不会产生ID
  218.                         lnkAttachment.Attributes.Add(
  219.                             "onclick",
  220.                             "javascript:" +
  221.                             "document.getElementById('" + this.RemovedFiles.ClientID + "');" +
  222.                             "document.getElementById('" + lnkAttachment.ClientID + "');");
  223.                         lnkAttachment.RenderControl(output);
  224.                         HyperLink lnkRemove = new HyperLink();
  225.                         this.Controls.Add(lnkRemove);
  226.                         lnkRemove.ImageUrl = ((SheetPage)this.Page).Enviroment.GetDeleteAttachmentImageUrl();
  227.                         lnkRemove.Attributes.Add("style", "cursor: hand");
  228.                         lnkRemove.Attributes.Add(
  229.                             "onclick",
  230.                             "javascript:" +
  231.                             "document.getElementById('" + this.RemovedFiles.ClientID + "').value += ('" + header.AttachmentID + ";');" +
  232.                             "document.getElementById('" + lnkAttachment.ClientID + "').style.display = 'none';" +
  233.                             "document.getElementById('" + lnkRemove.ClientID + "').style.display = 'none';");
  234.                         if (this.Enabled)
  235.                         {
  236.                             lnkRemove.RenderControl(output);
  237.                         }
  238.                         Label separator = new Label();
  239.                         separator.Text = "  ";
  240.                         separator.RenderControl(output);
  241.                     }
  242.                 }
  243.             }
  244.             // 记录删除的附件的控件
  245.             if (!this.DesignMode)
  246.             {
  247.                 this.RemovedFiles.RenderControl(output);
  248.             }
  249.             // 显示添加附件模块
  250.             if (this.Enabled)
  251.             {
  252.                 this.ListBox.Attributes.Add("style", "width:80%;");
  253.             
  254.                 string divId = this.ClientID + "_DivAppend";
  255.                 string hideButton = this.ClientID + "_HideButton";
  256.                 string displayButton = this.ClientID + "_DisplayButton";
  257.                 // 隐藏显示按钮
  258.                 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';">");
  259.                 output.Write("<input type="button" name="" + displayButton + "" value="+" onClick="javascript:" + divId + ".style.display='block';" + hideButton + ".style.display='block';" + displayButton + ".style.display='none';">");
  260.                 output.Write("<div id="" + divId + "" style="display: none;">");
  261.                 string addinput = this.ClientID + "_addinput";
  262.                 string fileNum = this.ClientID + "_fileNum";
  263.                 string remove = this.ClientID + "_remove";
  264.                 string file = this.ClientID + "_file";
  265.                 string files = this.ClientID + "_files";
  266.                 if (!this.Page.ClientScript.IsClientScriptBlockRegistered("SheetMultiAttachmentScript"))
  267.                 {
  268.                     output.Write("<script type="text/javascript">" + "rn");
  269.                     output.Write("var " + fileNum + " = 0;" + "rn");
  270.                     // 添加
  271.                     output.Write("function " + addinput + "()" + "rn");
  272.                     output.Write("{" + "rn");
  273.                     output.Write("//隐藏File控件" + "rn");
  274.                     output.Write("var ofile = document.getElementById("" + file + "" + " + fileNum + ");" + "rn");
  275.                     output.Write("if(ofile.value=='')" + "rn");
  276.                     output.Write("{" + "rn");
  277.                     output.Write("return;" + "rn");
  278.                     output.Write("}" + "rn");
  279.                     output.Write("// 检查是否重复" + "rn");
  280.                     output.Write("var myOptions=document.getElementById("" + this.ListBox.ClientID + "").options;" + "rn");
  281.                     output.Write("for (i=0; i<myOptions.length; i++)" + "rn");
  282.                     output.Write("{" + "rn");
  283.                     output.Write("if (myOptions[i].text == ofile.value)" + "rn");
  284.                     output.Write("{" + "rn");
  285.                     output.Write("alert("该文件已存在列表当中,请不要重复选择!");" + "rn");
  286.                     output.Write("return;" + "rn");
  287.                     output.Write("}" + "rn");
  288.                     output.Write("}" + "rn");
  289.                     output.Write("ofile.style.display = 'none';" + "rn");
  290.                     output.Write("//创建新file控件" + "rn");
  291.                     output.Write("var f = document.createElement("input");" + "rn");
  292.                     output.Write("" + fileNum + " = " + fileNum + " + 1;" + "rn");
  293.                     output.Write("f.type = 'file';" + "rn");
  294.                     output.Write("f.name = '" + file + "'+" + fileNum + ";" + "rn");
  295.                     output.Write("f.id = '" + file + "'+" + fileNum + ";" + "rn");
  296.                     output.Write("f.size = '20';" + "rn");
  297.                     output.Write("f.className = 'in3';" + "rn");
  298.                     output.Write("var div1 = document.getElementById("" + files + "")" + "rn");
  299.                     output.Write("div1.insertAdjacentElement("BeforeBegin", f);" + "rn");
  300.                     output.Write("// 添加到列表中" + "rn");
  301.                     output.Write("var mySelect = document.getElementById("" + this.ListBox.ClientID + "");" + "rn");
  302.                     output.Write("var option = new Option(ofile.value, ofile.name);" + "rn");
  303.                     output.Write("mySelect.options.add(option);" + "rn");
  304.                     output.Write("mySelect.options.selectedIndex = mySelect.options.length-1;" + "rn");
  305.                     // output.Write("mySelect.size++;" + "rn");
  306.                     output.Write("}" + "rn");
  307.                     output.Write("function " + remove + "()" + "rn");
  308.                     output.Write("{" + "rn");
  309.                     output.Write("var mySelect=document.getElementById("" + this.ListBox.ClientID + "");" + "rn");
  310.                     output.Write("var selectedIndex=mySelect.selectedIndex;" + "rn");
  311.                     output.Write("if (selectedIndex == -1)" + "rn");
  312.                     output.Write("{" + "rn");
  313.                     output.Write("alert("请选择要移除的文件!");" + "rn");
  314.                     output.Write("return;" + "rn");
  315.                     output.Write("}" + "rn");
  316.                     output.Write("var inputID = mySelect.options[selectedIndex].value;" + "rn");
  317.                     output.Write("mySelect.options.remove(selectedIndex);" + "rn");
  318.                     output.Write("if(selectedIndex == 0)" + "rn");
  319.                     output.Write("{" + "rn");
  320.                     output.Write("mySelect.options.selectedIndex=0;" + "rn");
  321.                     output.Write("}" + "rn");
  322.                     output.Write("else" + "rn");
  323.                     output.Write("{" + "rn");
  324.                     output.Write("mySelect.options.selectedIndex=selectedIndex-1;" + "rn");
  325.                     output.Write("}" + "rn");
  326.                     // output.Write("mySelect.size--;" + "rn");
  327.                     output.Write("//非电子文件,移出input按钮" + "rn");
  328.                     output.Write("if (inputID.lastIndexOf("\\")==-1)" + "rn");
  329.                     output.Write("{" + "rn");
  330.                     output.Write("var objinput = document.getElementById(inputID);" + "rn");
  331.                     output.Write("objinput.removeNode(true);" + "rn");
  332.                     output.Write("}" + "rn");
  333.                     output.Write("else" + "rn");
  334.                     output.Write("{" + "rn");
  335.                     output.Write("//删除隐藏域的一个值" + "rn");
  336.                     output.Write("var obj= document.getElementById("" + this.HiddenField.ClientID + "");" + "rn");
  337.                     output.Write("var str=obj.value;" + "rn");
  338.                     output.Write("var Files =new Array();" + "rn");
  339.                     output.Write("Files=str.split('|');" + "rn");
  340.                     output.Write("for (var i=0; i<Files.length;i++)" + "rn");
  341.                     output.Write("{" + "rn");
  342.                     output.Write("if (inputID == Files[i])" + "rn");
  343.                     output.Write("{" + "rn");
  344.                     output.Write("str=str.replace(inputID,"");" + "rn");
  345.                     output.Write("obj.value=str;" + "rn");
  346.                     output.Write("return;" + "rn");
  347.                     output.Write("}" + "rn");
  348.                     output.Write("}" + "rn");
  349.                     output.Write("}" + "rn");
  350.                     output.Write("}" + "rn");
  351.                     output.Write("</script>" + "rn");
  352.                     this.Page.ClientScript.RegisterClientScriptBlock(typeof(SheetAttachment), "SheetMultiAttachmentScript", "");
  353.                 }
  354.                 output.Write("<table width="100%" border="0">" + "rn");
  355.                 output.Write("<tr>" + "rn");
  356.                 output.Write("<td>" + "rn");
  357.                 output.Write("<table width="100%">" + "rn");
  358.                 output.Write("<tr>" + "rn");
  359.                 output.Write("<td>" + "rn");
  360.                 this.ListBox.RenderControl(output);
  361.                 output.Write("<input id="Button3" type="button" class="btn" value="删除" onclick="" + remove + "();"/>" + "rn");
  362.                 this.HiddenField.RenderControl(output);
  363.                 output.Write("</td>" + "rn");
  364.                 output.Write("</tr>" + "rn");
  365.                 output.Write("<tr>" + "rn");
  366.                 output.Write("<td>" + "rn");
  367.                 output.Write("<div id="" + files + "">" + "rn");
  368.                 this.FileUpload.ID = file + "0";
  369.                 this.FileUpload.RenderControl(output);
  370.                 output.Write("</div>" + "rn");
  371.                 output.Write("<input id="Button2" class="btn" type="button" value="添加文件" onclick="" + addinput + "();" />" + "rn");
  372.                 output.Write("</td>" + "rn");
  373.                 output.Write("</tr>" + "rn");
  374.                 output.Write("</table>" + "rn");
  375.                 output.Write("</td>" + "rn");
  376.                 output.Write("</tr>" + "rn");
  377.                 output.Write("</table>" + "rn");
  378.                 output.Write("</div>");
  379.             }
  380.         }
  381.     }
  382. }