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

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_personnel_curricula_vitae_upload_photo : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     protected void BtnUpload_Click(object sender, EventArgs e)
  18.     {
  19.         DateMgr mgr = new DateMgr();
  20.         string time = mgr.getTime();
  21.         if (this.uploadFile(time) && Request.QueryString["e_id"] != null)
  22.         {
  23.             string save_name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  24.             string name = FileUpload1.FileName;
  25.             SqlConnection conn = dbConnection.getConnection();
  26.             conn.Open();
  27.             SqlCommand cmd = new SqlCommand("update OA_EMPLOYEE set photo_name='"+name+
  28.                 "', photo_save_name='"+save_name+"' where id="+Request.QueryString["e_id"], conn);
  29.             cmd.ExecuteNonQuery();
  30.             cmd = new SqlCommand("select emp_name from OA_EMPLOYEE where id="+Request.QueryString["e_id"], conn);
  31.             HlEmployeeShow.Text = "查看" + cmd.ExecuteScalar().ToString() + "的資料";
  32.             HlEmployeeShow.NavigateUrl = "employee_show.aspx?e_id="+Request.QueryString["e_id"];
  33.             HlEmployeeList.Text = "查看本部門員工列表";
  34.             HlEmployeeList.NavigateUrl = "employee_list.aspx";
  35.             conn.Close();
  36.             Response.Write("<script language='javascript'>alert('上傳檔案成功');</script>");
  37.             Photo.ImageUrl = "../../employee_photo/" + save_name;
  38.             
  39.         }
  40.     }
  41.     protected bool uploadFile(string time)
  42.     {
  43.         bool flag = false;
  44.         string directory = Server.MapPath("~/web/employee_photo/");
  45.         Response.Write("<script language='javascript'>alert('"+directory+"');</script>");
  46.         if (FileUpload1.HasFile)
  47.         {
  48.             bool extenstion = false;
  49.             string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  50.             string[] allowedExtensions = 
  51.                 { ".gif", ".jpg" };
  52.             for (int i = 0; i < allowedExtensions.Length; i++)
  53.             {
  54.                 if (fileExtension == allowedExtensions[i])
  55.                 {
  56.                     extenstion = true;
  57.                     break;
  58.                 }
  59.             }
  60.             if (!extenstion)
  61.             {
  62.                 Response.Write("<script language='javascript'>alert('只允許上傳格式為gif, jpg的檔案!');</script>");
  63.                 return false;
  64.             }
  65.             else
  66.             {
  67.                 string name = FileUpload1.FileName.Substring(0, FileUpload1.FileName.Length - 4) + time + System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  68.                 FileUpload1.PostedFile.SaveAs(directory + name);
  69.                 ContentLength.Value = FileUpload1.PostedFile.ContentLength.ToString();
  70.                 flag = true;
  71.             }
  72.         }
  73.         return flag;
  74.     }
  75.     protected void BtnUnUpload_Click(object sender, EventArgs e)
  76.     {
  77.         Response.Redirect("employee_list_index.aspx");
  78.     }
  79. }