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

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_report_add_report : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     protected void btnOK_Click(object sender, EventArgs e)
  18.     {
  19.         if (Employee_ids.Value.Equals(""))
  20.         {
  21.             LblReceivers.Text = "收件人不能為空";
  22.         }
  23.         else
  24.         {
  25.             DateMgr date = new DateMgr();
  26.             string time = date.getTime();
  27.             //上傳檔案
  28.             if (!this.uploadFile(time))
  29.             {
  30.                 if (FileUpload1.HasFile)
  31.                 {
  32.                     //上傳檔案不成功
  33.                 }
  34.                 else
  35.                 {
  36.                     //沒有上傳附件
  37.                     this.saveReport();
  38.                 }
  39.             }
  40.             else
  41.             {
  42.                 this.saveReport();
  43.             }
  44.         }
  45.     }
  46.     protected bool uploadFile(string time)
  47.     {
  48.         bool flag = false;
  49.         string server_ip = "";
  50.         string root = "";
  51.         string folder = "";
  52.         if (Application["FILE_SERVER_IP"] != null)
  53.         {
  54.             server_ip = Application["FILE_SERVER_IP"].ToString();
  55.         }
  56.         if (Application["FILE_SERVER_ROOT"] != null)
  57.         {
  58.             root = Application["FILE_SERVER_ROOT"].ToString();
  59.         }
  60.         if (Application["REPORT_FOLDER"] != null)
  61.         {
  62.             folder = Application["REPORT_FOLDER"].ToString();
  63.         }
  64.         string directory = "d:\oa(new)\web\oa_upload\report\";
  65.         if (FileUpload1.HasFile)
  66.         {
  67.             bool extenstion = false;
  68.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  69.             string[] allowedExtensions = 
  70.                 { ".gif", ".jpg", ".doc", ".xls", ".rar", ".zip", ".txt", ".vsd", ".vss", ".vst", ".vdx", ".vsx", ".vtx" };
  71.             for (int i = 0; i < allowedExtensions.Length; i++)
  72.             {
  73.                 if (fileExtension == allowedExtensions[i])
  74.                 {
  75.                     extenstion = true;
  76.                     break;
  77.                 }
  78.             }
  79.             if (!extenstion)
  80.             {
  81.                 LblFileError.Text = "只允許上傳格式為gif, jpg, doc, xls, rar, zip, txt, vsd, vss, vst, vdx, vsx, vtx的檔案";
  82.                 return false;
  83.             }
  84.             else
  85.             {
  86.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  87.                 FileUpload1.PostedFile.SaveAs(directory + name);
  88.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  89.                 flag = true;
  90.             }
  91.         }
  92.         return flag;
  93.     }
  94.     protected void saveReport()
  95.     {
  96.         //添加數据
  97.         SqlConnection conn = dbConnection.getConnection();
  98.         conn.Open();
  99.         int report_id = 1;
  100.         SqlCommand cmd = new SqlCommand("select max(id) from OA_REPORT_CONTENT", conn);
  101.         try
  102.         {
  103.             report_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  104.         }
  105.         catch { }
  106.         int sender_id = 1;
  107.         cmd = new SqlCommand("select max(id) from OA_REPORT_SENDER", conn);
  108.         try
  109.         {
  110.             sender_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  111.         }
  112.         catch { }
  113.         int receiver_id = 1;
  114.         cmd = new SqlCommand("select max(id) from OA_REPORT_RECEIVER", conn);
  115.         try
  116.         {
  117.             receiver_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  118.         }
  119.         catch { }
  120.         int attachment_id = 1;
  121.         cmd = new SqlCommand("select max(id) from OA_REPORT_ATTACHMENT", conn);
  122.         try
  123.         {
  124.             attachment_id = Convert.ToInt32(cmd.ExecuteScalar()) + 1;
  125.         }
  126.         catch { }
  127.         SqlTransaction tx = conn.BeginTransaction();
  128.         try
  129.         {
  130.             //添加報告內容
  131.             cmd = new SqlCommand("insert into OA_REPORT_CONTENT(id, title, content, proposal, summarize) values (" +
  132.                 report_id + ", '" + TxtTitle.Text + "', '" + TxtContent.Text.Replace("n", "<br>").Replace(" ", "&nbsp") +
  133.                 "', '" + TxtProposal.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "', '" +
  134.                 TxtSummarize.Text.Replace("n", "<br>").Replace(" ", "&nbsp") + "')", conn);
  135.             cmd.Transaction = tx;
  136.             cmd.ExecuteNonQuery();
  137.             //添加發件人信息
  138.             int user_id = 0;
  139.             if (Session["user"] != null)
  140.             {
  141.                 user_id = ((User)Session["user"]).Id;
  142.             }
  143.             DateMgr mgr = new DateMgr();
  144.             string time = mgr.getDateTime();
  145.             cmd = new SqlCommand("insert into OA_REPORT_SENDER(id, report_id, sender, " +
  146.                 "is_del, send_date)values(" + sender_id + ", " + report_id + ", " + user_id +
  147.                 ", 0, '" + time + "')", conn);
  148.             cmd.Transaction = tx;
  149.             cmd.ExecuteNonQuery();
  150.             //添加收件人
  151.             if (!Employee_ids.Value.Equals(""))
  152.             {
  153.                 string[] receiver_ids = Employee_ids.Value.Split(new char[] { ';' });
  154.                 for (int i = 0; i < receiver_ids.Length; i++)
  155.                 {
  156.                     cmd = new SqlCommand("insert into OA_REPORT_RECEIVER(id, report_id, " +
  157.                         "receiver, is_read, is_del, read_date, sign, sign_date) values (" + receiver_id + ", " +
  158.                         report_id + ", " + receiver_ids[i] + ", 0, 0, '', '', '')", conn);
  159.                     cmd.Transaction = tx;
  160.                     cmd.ExecuteNonQuery();
  161.                     receiver_id++;
  162.                 }
  163.             }
  164.             //添加附件
  165.             DateMgr date = new DateMgr();
  166.             time = date.getTime();
  167.             string name = "";
  168.             try
  169.             {
  170.                 name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  171.             }
  172.             catch { }
  173.             cmd = new SqlCommand("insert into OA_REPORT_ATTACHMENT(id, report_id, name, content_length)" +
  174.                 " values (" + attachment_id + ", " + report_id + ", '" + name + "','" + ContentLength.Value + "')", conn);
  175.             cmd.Transaction = tx;
  176.             cmd.ExecuteNonQuery();
  177.             tx.Commit();
  178.             Response.Redirect("report_list_send.aspx");
  179.         }
  180.         catch (Exception ex)
  181.         {
  182.             Response.Write(ex.Message);
  183.           //  tx.Rollback();
  184.         }
  185.         conn.Close();
  186.     }
  187. }