dir.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:13k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. public partial class web_regulation_dir : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         LoginDAO.CheckLogin(Session, Response, "../", 2);
  17.         if (!IsPostBack)
  18.         {
  19.             if (Session["dir_id"] != null)
  20.             {
  21.                 SqlConnection conn = dbConnection.getConnection();
  22.                 conn.Open();
  23.                 SqlCommand cmd = new SqlCommand("select name from OA_SYS_CHILD_MENU where id=" + Session["dir_id"].ToString(), conn);
  24.                 LblTitle.Text = cmd.ExecuteScalar().ToString();
  25.                 LblLastDir.Text = LblTitle.Text;
  26.                 LblFileDir.Text = LblTitle.Text;
  27.                 DirId.Value = Session["dir_id"].ToString();
  28.                 conn.Close();
  29.             }
  30.             else
  31.             {
  32.                 SqlConnection conn = dbConnection.getConnection();
  33.                 conn.Open();
  34.                 SqlCommand cmd = new SqlCommand("select name from OA_SYS_CHILD_MENU where id=16", conn);
  35.                 LblTitle.Text = cmd.ExecuteScalar().ToString();
  36.                 conn.Close();
  37.                 LblLastDir.Text = LblTitle.Text;
  38.                 LblFileDir.Text = LblTitle.Text;
  39.                 ParentId.Value = "16";
  40.             }
  41.         }
  42.     }
  43.     protected void BtnOk_Click(object sender, EventArgs e)
  44.     {
  45.         int flag = 0;
  46.         if (TxtName.Text.Trim().Equals(""))
  47.         {
  48.             flag = 1;
  49.             LblErrDirName.Text = "目錄名不能為空";
  50.         }
  51.         if (flag == 0)
  52.         {
  53.             LblErrDirName.Text = "";
  54.             try
  55.             {
  56.                 SqlConnection conn = dbConnection.getConnection();
  57.                 conn.Open();
  58.                 int sequence = 1;
  59.                 int id = 1;
  60.                 SqlCommand cmd = new SqlCommand("select max(sequence) from OA_SYS_CHILD_MENU where parent_id=" + ParentId.Value, conn);
  61.                 try
  62.                 {
  63.                     sequence = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  64.                 }
  65.                 catch { }
  66.                 cmd = new SqlCommand("select max(id) from OA_SYS_CHILD_MENU", conn);
  67.                 try
  68.                 {
  69.                     id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  70.                 }
  71.                 catch { }
  72.                 string user = "";
  73.                 if (Session["user"] != null)
  74.                 {
  75.                     user = ((User)Session["user"]).Emp_name;
  76.                 }
  77.                 DateMgr mgr = new DateMgr();
  78.                 string time = mgr.getDateTime();
  79.                 string sql = "insert into OA_SYS_CHILD_MENU(id, name, description, menu_url, parent_id, target, sequence, " +
  80.                     "cre_user, cre_date, is_dir) values(" + id + ", '" + TxtName.Text + "','" + TxtDescription.Text +
  81.                     "','regulation/dir.aspx?dir_id=" + id + "','" + DirId.Value + "','mainFrame'," + sequence + ", '" + 
  82.                     user + "', '" + time + "','Y')";
  83.                 cmd = new SqlCommand(sql, conn);
  84.                 cmd.ExecuteNonQuery();
  85.                 conn.Close();
  86.                 TxtDescription.Text = "";
  87.                 TxtName.Text = "";
  88.                 DirList.DataBind();
  89.                 PnlDir.Visible = false;
  90.                 PnlDirList.Visible = true;
  91.                 //刷新樹
  92.                 Response.Write("<script language='javascript'>window.parent.frames.leftFrame.location.reload()</script>");
  93.             }
  94.             catch (Exception ex)
  95.             {
  96.                 Response.Write(ex.Message);
  97.             }
  98.         }
  99.     }
  100.     protected void BtnShowFile_Click(object sender, EventArgs e)
  101.     {
  102.         pnlAddFile.Visible = false;
  103.         PnlDirList.Visible = false;
  104.         PnlDir.Visible = false;
  105.         PnlFileList.Visible = true;
  106.     }
  107.     protected void BtnShowDir_Click(object sender, EventArgs e)
  108.     {
  109.         PnlDirList.Visible = true;
  110.         PnlDir.Visible = false;
  111.         pnlAddFile.Visible = false;
  112.         PnlFileList.Visible = false;
  113.     }
  114.     protected void BtnAddDir_Click(object sender, EventArgs e)
  115.     {
  116.         PnlDirList.Visible = false;
  117.         PnlDir.Visible = true;
  118.         pnlAddFile.Visible = false;
  119.         PnlFileList.Visible = false;
  120.     }
  121.     protected void BtnAddFile_Click(object sender, EventArgs e)
  122.     {
  123.         pnlAddFile.Visible = true;
  124.         PnlDirList.Visible = false;
  125.         PnlDir.Visible = false;
  126.         PnlFileList.Visible = false;
  127.     }
  128.     protected void BtnFileOk_Click(object sender, EventArgs e)
  129.     {
  130.         DateMgr date = new DateMgr();
  131.         string time = date.getTime();
  132.         
  133.         if (this.uploadFile(time))
  134.         {
  135.             try
  136.             {
  137.                 //上傳檔案成功, 儲存資料
  138.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  139.                 string real_name = FileUpload1.FileName;
  140.                 SqlConnection conn = dbConnection.getConnection();
  141.                 conn.Open();
  142.                 int id = 1;
  143.                 SqlCommand cmd = new SqlCommand("select max(id) from OA_REGULATION_FILE", conn);
  144.                 try
  145.                 {
  146.                     id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  147.                 }
  148.                 catch { }
  149.                 string user = "";
  150.                 if (Session["user"] != null)
  151.                 {
  152.                     user = ((User)Session["user"]).Emp_name;
  153.                 }
  154.                 DateMgr mgr = new DateMgr();
  155.                 time = mgr.getDateTime();
  156.                 cmd = new SqlCommand("insert into OA_REGULATION_FILE(id, name, real_name, content_length, " +
  157.                     "dir_id, cre_user, cre_date) values(" + id + ", '" + name + "', '" + real_name + "', '" +
  158.                     ContentLength.Value + "', '" + DirId.Value + "', '" + user + "', '" + time + "')", conn);
  159.                 cmd.ExecuteNonQuery();
  160.                 conn.Close();
  161.                 pnlAddFile.Visible = false;
  162.                 PnlFileList.Visible = true;
  163.                 FileList.DataBind();
  164.             }
  165.             catch (Exception ex)
  166.             {
  167.                 Response.Write(ex.Message);
  168.             }
  169.         }
  170.     }
  171.     protected bool uploadFile(string time)
  172.     {
  173.         bool flag = false;
  174.         string server_ip = "";
  175.         string root = "";
  176.         string folder = "";
  177.         if (Application["FILE_SERVER_IP"] != null)
  178.         {
  179.             server_ip = Application["FILE_SERVER_IP"].ToString();
  180.         }
  181.         if (Application["FILE_SERVER_ROOT"] != null)
  182.         {
  183.             root = Application["FILE_SERVER_ROOT"].ToString();
  184.         }
  185.         if (Application["REGULATION_FOLDER"] != null)
  186.         {
  187.             folder = Application["REGULATION_FOLDER"].ToString();
  188.         }
  189. //        string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
  190.         string directory = "D:\OA_UPLOAD\";
  191.         if (FileUpload1.HasFile)
  192.         {
  193.             bool extenstion = false;
  194.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  195.             string[] allowedExtensions = 
  196.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm"};
  197.             for (int i = 0; i < allowedExtensions.Length; i++)
  198.             {
  199.                 if (fileExtension == allowedExtensions[i])
  200.                 {
  201.                     extenstion = true;
  202.                     break;
  203.                 }
  204.             }
  205.             if (!extenstion)
  206.             {
  207.                 LblFileError.Text = "只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案";
  208.                 return false;
  209.             }
  210.             else
  211.             {
  212.                 LblFileError.Text = "";
  213.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  214.                 FileUpload1.PostedFile.SaveAs(directory + name);
  215.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  216.                 flag = true;
  217.             }
  218.         }
  219.         return flag;
  220.     }
  221.     protected void FileList_RowDataBound(object sender, GridViewRowEventArgs e)
  222.     {
  223.         //設置主鍵
  224.         FileList.DataKeyNames = new string[] { "id" };
  225.         //行高亮
  226.         if (e.Row.RowType == DataControlRowType.DataRow)
  227.         {
  228.             e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#c8dafa';");
  229.             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
  230.         }
  231.     }
  232.     protected void FileList_RowCreated(object sender, GridViewRowEventArgs e)
  233.     {
  234.         if (e.Row.RowType == DataControlRowType.DataRow)
  235.         {
  236.             LinkButton btnDelete = (LinkButton)e.Row.FindControl("BtnDelete");
  237.             btnDelete.CommandArgument = FileList.DataKeys[e.Row.RowIndex].Value.ToString();
  238.             LinkButton btnDownload = (LinkButton)e.Row.FindControl("BtnFile");
  239.             btnDownload.CommandArgument = FileList.DataKeys[e.Row.RowIndex].Value.ToString();
  240.         }
  241.     }
  242.     protected void FileList_RowCommand(object sender, GridViewCommandEventArgs e)
  243.     {
  244.         if (e.CommandName.Equals("DeleteData"))
  245.         {
  246.             string id = e.CommandArgument.ToString();
  247.             //點擊刪除按鈕
  248.             try
  249.             {
  250.                 SqlConnection conn = dbConnection.getConnection();
  251.                 conn.Open();
  252.                 SqlCommand cmd = new SqlCommand("delete from OA_REGULATION_FILE where id=" + id, conn);
  253.                 cmd.ExecuteNonQuery();
  254.                 FileList.DataBind();
  255.             }
  256.             catch (Exception ex)
  257.             {
  258.                 Response.Write(ex.Message);
  259.             }
  260.         }
  261.         if (e.CommandName.Equals("DownLoad"))
  262.         {
  263.             string id = e.CommandArgument.ToString();
  264.             //點擊下載按鈕
  265.             try
  266.             {
  267.                 SqlConnection conn = dbConnection.getConnection();
  268.                 conn.Open();
  269.                 SqlCommand cmd = new SqlCommand("select name, real_name, content_length from OA_REGULATION_FILE where id="+id, conn);
  270.                 SqlDataReader dr = cmd.ExecuteReader();
  271.                 if (dr.Read())
  272.                 {
  273.                     this.DownLoadFile(dr["name"].ToString(), dr["real_name"].ToString(), dr["content_length"].ToString());
  274.                 }
  275.                 dr.Close();
  276.                 conn.Close();
  277.             }
  278.             catch (Exception ex)
  279.             {
  280.                 Response.Write(ex.Message);
  281.             }
  282.         }
  283.     }
  284.     protected void DownLoadFile(string name, string real_name, string content_length)
  285.     {
  286.         string server_ip = "";
  287.         string root = "";
  288.         string folder = "";
  289.         if (Application["FILE_SERVER_IP"] != null)
  290.         {
  291.             server_ip = Application["FILE_SERVER_IP"].ToString();
  292.         }
  293.         if (Application["FILE_SERVER_ROOT"] != null)
  294.         {
  295.             root = Application["FILE_SERVER_ROOT"].ToString();
  296.         }
  297.         if (Application["REGULATION_FOLDER"] != null)
  298.         {
  299.             folder = Application["REGULATION_FOLDER"].ToString();
  300.         }
  301.         //string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
  302.         string directory = "D:\OA_UPLOAD\";
  303.         try
  304.         {
  305.             Response.Clear();
  306.             Response.ClearHeaders();
  307.             Response.Buffer = false;
  308.             Response.ContentType = "application/octet-stream";
  309.             Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(real_name, System.Text.Encoding.UTF8));
  310.             Response.AppendHeader("Content-Length", content_length);
  311.             Response.WriteFile(directory + name);
  312.             Response.Flush();
  313.             Response.End();
  314.         }
  315.         catch (Exception ex)
  316.         {
  317.             Response.Write(ex.Message);
  318.         }
  319.     }
  320.     protected void DirList_RowUpdated(object sender, GridViewUpdatedEventArgs e)
  321.     {
  322.         //刷新樹
  323.         Response.Write("<script language='javascript'>window.parent.frames.leftFrame.location.reload()</script>");
  324.     }
  325. }