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

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_file_add : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         if (!IsPostBack)
  17.         {
  18.             if (Session["dir_id"] != null)
  19.             {
  20.                 SqlConnection conn = dbConnection.getConnection();
  21.                 conn.Open();
  22.                 SqlCommand cmd = new SqlCommand("select name from OA_SYS_CHILD_MENU where id="+
  23.                     Session["dir_id"].ToString(), conn);
  24.                 LblFileDir.Text = cmd.ExecuteScalar().ToString();
  25.                 conn.Close();
  26.             }
  27.         }
  28.     }
  29.     protected void BtnFileOk_Click(object sender, EventArgs e)
  30.     {
  31.         DateMgr date = new DateMgr();
  32.         string time = date.getTime();
  33.         if (Session["dir_id"] != null)
  34.         {
  35.             int dir_id = Convert.ToInt32(Session["dir_id"].ToString());
  36.             if (this.uploadFile(time))
  37.             {
  38.                 try
  39.                 {
  40.                     //上傳檔案成功, 儲存資料
  41.                     string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  42.                     string real_name = FileUpload1.FileName;
  43.                     SqlConnection conn = dbConnection.getConnection();
  44.                     conn.Open();
  45.                     int id = 1;
  46.                     SqlCommand cmd = new SqlCommand("select max(id) from OA_REGULATION_FILE", conn);
  47.                     try
  48.                     {
  49.                         id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  50.                     }
  51.                     catch { }
  52.                     string user = "";
  53.                     if (Session["user"] != null)
  54.                     {
  55.                         user = ((User)Session["user"]).Emp_name;
  56.                     }
  57.                     DateMgr mgr = new DateMgr();
  58.                     time = mgr.getDateTime();
  59.                     cmd = new SqlCommand("insert into OA_REGULATION_FILE(id, name, real_name, content_length, " +
  60.                         "dir_id, cre_user, cre_date) values(" + id + ", '" + name + "', '" + real_name + "', '" +
  61.                         ContentLength.Value + "', '" + dir_id + "', '" + user + "', '" + time + "')", conn);
  62.                     cmd.ExecuteNonQuery();
  63.                     conn.Close();
  64.                     Response.Redirect("file_list.aspx");
  65.                 }
  66.                 catch (Exception ex)
  67.                 {
  68.                     Response.Write(ex.Message);
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     protected bool uploadFile(string time)
  74.     {
  75.         bool flag = false;
  76.         string server_ip = "";
  77.         string root = "";
  78.         string folder = "";
  79.         if (Application["FILE_SERVER_IP"] != null)
  80.         {
  81.             server_ip = Application["FILE_SERVER_IP"].ToString();
  82.         }
  83.         if (Application["FILE_SERVER_ROOT"] != null)
  84.         {
  85.             root = Application["FILE_SERVER_ROOT"].ToString();
  86.         }
  87.         if (Application["REGULATION_FOLDER"] != null)
  88.         {
  89.             folder = Application["REGULATION_FOLDER"].ToString();
  90.         }
  91.         //        string directory = "\\" + server_ip + "\" + root + "\" + folder + "\";
  92.         string directory = "d:\oa\web\oa_upload\resource_share\";
  93.         if (FileUpload1.HasFile)
  94.         {
  95.             bool extenstion = false;
  96.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  97.             string[] allowedExtensions = 
  98.                 { ".gif", ".jpg", ".doc", ".xls", ".rar",".pdf", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx", ".html", ".htm" };
  99.             for (int i = 0; i < allowedExtensions.Length; i++)
  100.             {
  101.                 if (fileExtension == allowedExtensions[i])
  102.                 {
  103.                     extenstion = true;
  104.                     break;
  105.                 }
  106.             }
  107.             if (!extenstion)
  108.             {
  109.                 Response.Write("只允許上傳格式為gif, jpg, doc, xls, rar,pdf, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案");
  110.                 return false;
  111.             }
  112.             else
  113.             {
  114.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  115.                 FileUpload1.PostedFile.SaveAs(directory + name);
  116.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  117.                 flag = true;
  118.             }
  119.         }
  120.         return flag;
  121.     }
  122. }