DeliverDocument.aspx.cs
上传用户:cha0314
上传日期:2014-03-02
资源大小:12522k
文件大小:8k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using UDS.Components;
  13. namespace UDS.SubModule.UnitiveDocument.Document
  14. {
  15. /// <summary>
  16. /// DeliverDocument 的摘要说明。
  17. /// </summary>
  18. public class DeliverDocument : System.Web.UI.Page
  19. {
  20. protected System.Web.UI.WebControls.Button btnUpload;
  21. protected System.Web.UI.WebControls.Button btnRemove;
  22. protected System.Web.UI.WebControls.Button btnSubmit;
  23. protected System.Web.UI.WebControls.Button btnReturn;
  24. protected System.Web.UI.HtmlControls.HtmlTextArea txtContent;
  25. protected System.Web.UI.HtmlControls.HtmlInputText txtAuthor;
  26. protected System.Web.UI.HtmlControls.HtmlInputText txtFrom;
  27. protected System.Web.UI.HtmlControls.HtmlInputText txtTitle;
  28. protected static string Username;
  29. public string ClassID;
  30. protected System.Web.UI.HtmlControls.HtmlInputFile File1;
  31. protected System.Web.UI.WebControls.ListBox listUpDoc;
  32. protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
  33. public  ArrayList listattfile= new ArrayList();
  34. private void Page_Load(object sender, System.EventArgs e)
  35. {
  36. if(!Page.IsPostBack)
  37. {
  38. Session["listattfile"]=listattfile;
  39. }
  40. ClassID  = (Request.QueryString["ClassID"]!=null)?Request.QueryString["ClassID"].ToString():"";
  41. HttpCookie UserCookie = Request.Cookies["Username"];
  42. Username = UserCookie.Value.ToString();
  43. }
  44. #region 处理表单post数据
  45. /// <summary>
  46. /// 处理表单post数据
  47. /// </summary>
  48. private DocBody ProcessFormPost()
  49. {
  50. ProjectClass pjt  = new ProjectClass ();
  51. int cstRightToApproveDocument = 2;
  52. // 验证
  53. if(Request.IsAuthenticated)
  54. {
  55. // 处理表单传递参数
  56. DocBody  docbody = new DocBody();
  57. docbody.DocTitle = this.txtTitle.Value;
  58. docbody.DocContent = this.txtContent.Value;
  59. docbody.DocAddedBy = Username;
  60. docbody.DocClassID = Int32.Parse(ClassID);
  61. docbody.DocAddedDate        = DateTime.Now.ToString();
  62. docbody.DocApprover = (pjt.GetAccessPermission(Int32.Parse(ClassID),Username,cstRightToApproveDocument))?Username:"";
  63. docbody.DocApproveDate = (pjt.GetAccessPermission(Int32.Parse(ClassID),Username,cstRightToApproveDocument))?DateTime.Now.ToString():"";
  64. docbody.DocApproved         = (docbody.DocApprover =="")?0:1;
  65. docbody.DocAttribute        = 0;
  66. docbody.DocType = 0;
  67. return docbody;
  68. }
  69. else
  70. {
  71. return null;
  72. }
  73. }
  74. #endregion
  75. #region 处理附件上传操作,传回附件数组
  76. /// <summary>
  77. /// 处理上传操作,附件以链接形式存放
  78. /// </summary>
  79. private ArrayList AttUpload()
  80. {
  81. HtmlForm FrmCompose   = (HtmlForm)this.Page.FindControl("DeliverDocument");
  82. Random TempNameInt    = new Random(); 
  83. string NewDocDirName  = TempNameInt.Next(100000000).ToString();
  84. ArrayList listattfile = (ArrayList)Session["listattfile"];
  85. // 存放附件至提交人目录中,随机生成目录名
  86. string FileName       = "";
  87. try
  88. if(Directory.Exists(Server.MapPath(".")+"\AttachFiles\tmp"))
  89. {
  90. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\tmp\"+NewDocDirName+Username);
  91. for (int i=0;i<FrmCompose.Controls.Count;i++)
  92. {
  93. if(FrmCompose.Controls [i].GetType().ToString()=="System.Web.UI.HtmlControls.HtmlInputFile")
  94. {
  95. HtmlInputFile hif = ((HtmlInputFile)(FrmCompose.Controls[i]));
  96. if(hif.PostedFile.FileName.Trim()!="")
  97. {
  98. FileName = System.IO .Path.GetFileName(hif.PostedFile.FileName);
  99. UDS.Components.DocAttachFile att = new UDS.Components.DocAttachFile();
  100. // 初始化
  101. att.FileAttribute  = 0;
  102. att.FileSize       = hif.PostedFile.ContentLength;
  103. att.FileName    = FileName;
  104. att.FileAuthor     = Username;
  105. att.FileCatlog     = "文档";
  106. att.FileVisualPath = "AttachFiles\tmp\"+NewDocDirName+Username+"\"+FileName;
  107. hif.PostedFile.SaveAs(Server.MapPath(".")+"\AttachFiles\tmp\"+NewDocDirName+Username+"\"+FileName);
  108. listattfile.Add(att);
  109. }
  110. hif=null;
  111. }
  112. }
  113. }
  114. else
  115. {   
  116. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\tmp");
  117. AttUpload();
  118. }
  119. }
  120. catch(Exception ioex)
  121. {
  122. UDS.Components.Error.Log(ioex.ToString());
  123. Server.Transfer("../../Error.aspx");
  124. }
  125. Session["listattfile"] = listattfile;
  126. return listattfile;
  127.             
  128. }
  129. #endregion
  130. #region Web Form Designer generated code
  131. override protected void OnInit(EventArgs e)
  132. {
  133. //
  134. // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
  135. //
  136. InitializeComponent();
  137. base.OnInit(e);
  138. }
  139. /// <summary>
  140. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  141. /// 此方法的内容。
  142. /// </summary>
  143. private void InitializeComponent()
  144. {    
  145. this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
  146. this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
  147. this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
  148. this.btnReturn.Click += new System.EventHandler(this.btnReturn_Click);
  149. this.Load += new System.EventHandler(this.Page_Load);
  150. }
  151. #endregion
  152. private void btnUpload_Click(object sender, System.EventArgs e)
  153. {
  154. AttUpload();
  155. this.listUpDoc.Items.Clear();
  156. int count = 0;
  157. ArrayList listattfile = (ArrayList)Session["listattfile"];
  158. foreach(UDS.Components.DocAttachFile att in listattfile)
  159. {
  160. count++;
  161. this.listUpDoc.Items.Add(new ListItem(att.FileName.ToString(),count.ToString()));
  162. }
  163. private void btnRemove_Click(object sender, System.EventArgs e)
  164. {
  165. ArrayList listattfile = (ArrayList)Session["listattfile"];
  166. for(int i=listUpDoc.Items.Count-1;i>=0;i--)
  167. {
  168. if(this.listUpDoc.Items[i].Selected)
  169. {
  170. this.listUpDoc.Items.RemoveAt(i);
  171. listattfile.RemoveAt (i);
  172. }
  173. }
  174. Session["listattfile"] = listattfile;
  175. }
  176. private void btnSubmit_Click(object sender, System.EventArgs e)
  177. {
  178. string DocID = "";
  179. DocBody docbody = ProcessFormPost();
  180. DocumentClass doc = new DocumentClass();
  181. // 添加文档,并返回ID
  182. DocID = doc.AddDocBody(docbody);
  183. ArrayList listattfile = (ArrayList)Session["listattfile"];
  184. foreach(UDS.Components.DocAttachFile att in listattfile)
  185. {
  186. try
  187. {
  188. if(!Directory.Exists(Server.MapPath(".")+"\AttachFiles\"+Username))
  189. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\"+Username);
  190. //Directory.Move(att.FileVisualPath.ToString().Replace(att.FileName,""),Server.MapPath(".")+"\AttachFiles\"+Username+"\");
  191. Random TempNameInt    = new Random(); 
  192. string NewDocDirName  = TempNameInt.Next(100000000).ToString();
  193. Directory.CreateDirectory(Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewDocDirName);
  194. File.Move(Server.MapPath(".")+"\"+att.FileVisualPath ,Server.MapPath(".")+"\AttachFiles\"+Username+"\"+NewDocDirName+"\"+att.FileName);
  195. Directory.Delete(Server.MapPath(".")+"\"+att.FileVisualPath.ToString().Replace(att.FileName,""),true);
  196. att.FileVisualPath    = "\AttachFiles\"+Username+"\"+NewDocDirName+"\"+att.FileName;
  197. att.FileAddedDate     = DateTime.Now.ToString();
  198. // 附件数据库处理
  199. doc.AddAttach(att,Int32.Parse(DocID));
  200. }
  201. catch(Exception ioex)
  202. {
  203. UDS.Components.Error.Log(ioex.ToString());
  204. Server.Transfer("../../Error.aspx");
  205. }
  206. }
  207. Response.Write("<script language=javascript>alert('文档提交成功!');self.location='../Switch.aspx?Action=1&ClassID="+ClassID+"';</script>");
  208. }
  209. private void btnReturn_Click(object sender, System.EventArgs e)
  210. {
  211. Response.Redirect("../Switch.aspx?Action=1&ClassID="+ClassID);
  212. }
  213. }
  214. }