file_add.aspx.cs
资源名称:web.rar [点击查看]
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:5k
源码类别:
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_file_add : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- 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);
- LblFileDir.Text = cmd.ExecuteScalar().ToString();
- conn.Close();
- }
- }
- }
- protected void BtnFileOk_Click(object sender, EventArgs e)
- {
- DateMgr date = new DateMgr();
- string time = date.getTime();
- if (Session["dir_id"] != null)
- {
- int dir_id = Convert.ToInt32(Session["dir_id"].ToString());
- 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 + "', '" + dir_id + "', '" + user + "', '" + time + "')", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- Response.Redirect("file_list.aspx");
- }
- 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\web\oa_upload\resource_share\";
- if (FileUpload1.HasFile)
- {
- bool extenstion = false;
- string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- string[] allowedExtensions =
- { ".gif", ".jpg", ".doc", ".xls", ".rar",".pdf", ".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)
- {
- Response.Write("只允許上傳格式為gif, jpg, doc, xls, rar,pdf, zip, txt, vsd, vss, vst, vdx, vsx, vtx, html, htm的檔案");
- return false;
- }
- else
- {
- 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;
- }
- }