dir.aspx.cs
资源名称:web.rar [点击查看]
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:13k
源码类别:
OA系统
开发平台:
ASP/ASPX
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Data.SqlClient;
- public partial class web_regulation_dir : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- LoginDAO.CheckLogin(Session, Response, "../", 2);
- if (!IsPostBack)
- {
- if (Session["dir_id"] != null)
- {
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- SqlCommand cmd = new SqlCommand("select name from OA_SYS_CHILD_MENU where id=" + Session["dir_id"].ToString(), conn);
- LblTitle.Text = cmd.ExecuteScalar().ToString();
- LblLastDir.Text = LblTitle.Text;
- LblFileDir.Text = LblTitle.Text;
- DirId.Value = Session["dir_id"].ToString();
- conn.Close();
- }
- else
- {
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- SqlCommand cmd = new SqlCommand("select name from OA_SYS_CHILD_MENU where id=16", conn);
- LblTitle.Text = cmd.ExecuteScalar().ToString();
- conn.Close();
- LblLastDir.Text = LblTitle.Text;
- LblFileDir.Text = LblTitle.Text;
- ParentId.Value = "16";
- }
- }
- }
- protected void BtnOk_Click(object sender, EventArgs e)
- {
- int flag = 0;
- if (TxtName.Text.Trim().Equals(""))
- {
- flag = 1;
- LblErrDirName.Text = "目錄名不能為空";
- }
- if (flag == 0)
- {
- LblErrDirName.Text = "";
- try
- {
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- int sequence = 1;
- int id = 1;
- SqlCommand cmd = new SqlCommand("select max(sequence) from OA_SYS_CHILD_MENU where parent_id=" + ParentId.Value, conn);
- try
- {
- sequence = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- cmd = new SqlCommand("select max(id) from OA_SYS_CHILD_MENU", conn);
- try
- {
- id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- string user = "";
- if (Session["user"] != null)
- {
- user = ((User)Session["user"]).Emp_name;
- }
- DateMgr mgr = new DateMgr();
- string time = mgr.getDateTime();
- string sql = "insert into OA_SYS_CHILD_MENU(id, name, description, menu_url, parent_id, target, sequence, " +
- "cre_user, cre_date, is_dir) values(" + id + ", '" + TxtName.Text + "','" + TxtDescription.Text +
- "','regulation/dir.aspx?dir_id=" + id + "','" + DirId.Value + "','mainFrame'," + sequence + ", '" +
- user + "', '" + time + "','Y')";
- cmd = new SqlCommand(sql, conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- TxtDescription.Text = "";
- TxtName.Text = "";
- DirList.DataBind();
- PnlDir.Visible = false;
- PnlDirList.Visible = true;
- //刷新樹
- Response.Write("<script language='javascript'>window.parent.frames.leftFrame.location.reload()</script>");
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- }
- protected void BtnShowFile_Click(object sender, EventArgs e)
- {
- pnlAddFile.Visible = false;
- PnlDirList.Visible = false;
- PnlDir.Visible = false;
- PnlFileList.Visible = true;
- }
- protected void BtnShowDir_Click(object sender, EventArgs e)
- {
- PnlDirList.Visible = true;
- PnlDir.Visible = false;
- pnlAddFile.Visible = false;
- PnlFileList.Visible = false;
- }
- protected void BtnAddDir_Click(object sender, EventArgs e)
- {
- PnlDirList.Visible = false;
- PnlDir.Visible = true;
- pnlAddFile.Visible = false;
- PnlFileList.Visible = false;
- }
- protected void BtnAddFile_Click(object sender, EventArgs e)
- {
- pnlAddFile.Visible = true;
- PnlDirList.Visible = false;
- PnlDir.Visible = false;
- PnlFileList.Visible = false;
- }
- protected void BtnFileOk_Click(object sender, EventArgs e)
- {
- DateMgr date = new DateMgr();
- string time = date.getTime();
- if (this.uploadFile(time))
- {
- try
- {
- //上傳檔案成功, 儲存資料
- string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- string real_name = FileUpload1.FileName;
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- int id = 1;
- SqlCommand cmd = new SqlCommand("select max(id) from OA_REGULATION_FILE", conn);
- try
- {
- id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
- }
- catch { }
- string user = "";
- if (Session["user"] != null)
- {
- user = ((User)Session["user"]).Emp_name;
- }
- DateMgr mgr = new DateMgr();
- time = mgr.getDateTime();
- cmd = new SqlCommand("insert into OA_REGULATION_FILE(id, name, real_name, content_length, " +
- "dir_id, cre_user, cre_date) values(" + id + ", '" + name + "', '" + real_name + "', '" +
- ContentLength.Value + "', '" + DirId.Value + "', '" + user + "', '" + time + "')", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- pnlAddFile.Visible = false;
- PnlFileList.Visible = true;
- FileList.DataBind();
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- }
- protected bool uploadFile(string time)
- {
- bool flag = false;
- string server_ip = "";
- string root = "";
- string folder = "";
- if (Application["FILE_SERVER_IP"] != null)
- {
- server_ip = Application["FILE_SERVER_IP"].ToString();
- }
- if (Application["FILE_SERVER_ROOT"] != null)
- {
- root = Application["FILE_SERVER_ROOT"].ToString();
- }
- if (Application["REGULATION_FOLDER"] != null)
- {
- folder = Application["REGULATION_FOLDER"].ToString();
- }
- // string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
- string directory = "D:\OA_UPLOAD\";
- if (FileUpload1.HasFile)
- {
- bool extenstion = false;
- string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- string[] allowedExtensions =
- { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm"};
- for (int i = 0; i < allowedExtensions.Length; i++)
- {
- if (fileExtension == allowedExtensions[i])
- {
- extenstion = true;
- break;
- }
- }
- if (!extenstion)
- {
- LblFileError.Text = "只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案";
- return false;
- }
- else
- {
- LblFileError.Text = "";
- string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- FileUpload1.PostedFile.SaveAs(directory + name);
- ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
- flag = true;
- }
- }
- return flag;
- }
- protected void FileList_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- //設置主鍵
- FileList.DataKeyNames = new string[] { "id" };
- //行高亮
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor; this.style.backgroundColor='#c8dafa';");
- e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
- }
- }
- protected void FileList_RowCreated(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- LinkButton btnDelete = (LinkButton)e.Row.FindControl("BtnDelete");
- btnDelete.CommandArgument = FileList.DataKeys[e.Row.RowIndex].Value.ToString();
- LinkButton btnDownload = (LinkButton)e.Row.FindControl("BtnFile");
- btnDownload.CommandArgument = FileList.DataKeys[e.Row.RowIndex].Value.ToString();
- }
- }
- protected void FileList_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName.Equals("DeleteData"))
- {
- string id = e.CommandArgument.ToString();
- //點擊刪除按鈕
- try
- {
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- SqlCommand cmd = new SqlCommand("delete from OA_REGULATION_FILE where id=" + id, conn);
- cmd.ExecuteNonQuery();
- FileList.DataBind();
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- if (e.CommandName.Equals("DownLoad"))
- {
- string id = e.CommandArgument.ToString();
- //點擊下載按鈕
- try
- {
- SqlConnection conn = dbConnection.getConnection();
- conn.Open();
- SqlCommand cmd = new SqlCommand("select name, real_name, content_length from OA_REGULATION_FILE where id="+id, conn);
- SqlDataReader dr = cmd.ExecuteReader();
- if (dr.Read())
- {
- this.DownLoadFile(dr["name"].ToString(), dr["real_name"].ToString(), dr["content_length"].ToString());
- }
- dr.Close();
- conn.Close();
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- }
- protected void DownLoadFile(string name, string real_name, string content_length)
- {
- string server_ip = "";
- string root = "";
- string folder = "";
- if (Application["FILE_SERVER_IP"] != null)
- {
- server_ip = Application["FILE_SERVER_IP"].ToString();
- }
- if (Application["FILE_SERVER_ROOT"] != null)
- {
- root = Application["FILE_SERVER_ROOT"].ToString();
- }
- if (Application["REGULATION_FOLDER"] != null)
- {
- folder = Application["REGULATION_FOLDER"].ToString();
- }
- //string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
- string directory = "D:\OA_UPLOAD\";
- try
- {
- Response.Clear();
- Response.ClearHeaders();
- Response.Buffer = false;
- Response.ContentType = "application/octet-stream";
- Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(real_name, System.Text.Encoding.UTF8));
- Response.AppendHeader("Content-Length", content_length);
- Response.WriteFile(directory + name);
- Response.Flush();
- Response.End();
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- protected void DirList_RowUpdated(object sender, GridViewUpdatedEventArgs e)
- {
- //刷新樹
- Response.Write("<script language='javascript'>window.parent.frames.leftFrame.location.reload()</script>");
- }
- }