?? admin_add.aspx.cs
字號:
?using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class Admin_Admin_Add : System.Web.UI.Page
{
private Int32 FileLength = 0;
OleDbConnection MyConn;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserID"] == "" || Session["UserID"] == null)
{
Response.Write("<script language=javascript>window.alert('為了系統安全,請您重新登陸');window.location.href=('Admin_Login.aspx')</script>");
}
}
protected void BtnSubmit_Click(object sender, EventArgs e)
{
{
try
{
MyConn = DB.CreateDB();
string title =FunStr(this.TxtTitle.Text);
string content =FunStr(this.TxtContent.Text);
string ptype = Request.QueryString["action"].ToString();
string FileName = myFile.Value;
HttpPostedFile UpFile = myFile.PostedFile; //獲取對由客戶端指定的上傳文件的訪問
FileLength = UpFile.ContentLength;//獲取上傳文件的字節大小
if (FileLength == 0)
{
MyConn.Open();
OleDbCommand cmd1 = new OleDbCommand("insert into product(pro_name,title,content) values('" + ptype + "','" + title + "','" + content + "')",MyConn);
cmd1.ExecuteNonQuery();
Response.Write("<script>alert('數據添加成功');window.location.href='Admin_Add.aspx?action=" + ptype + "'</script>");
MyConn.Close();
}
else
{
string exName = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();//截取圖片的后綴名
if (exName == "JPG" || exName == "BMP" || exName == "GIF")//判斷圖片的類型
{
if (FileLength > 204800)//判斷圖片是否大于200k(根據自己的需要判斷大小)
{
Response.Write("<script>alert('對不起,圖片大小不能大于200K');window.location.href='Admin_Add.aspx?action=" + ptype + "'</script>");
return;
}
else
{
string ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + exName;//圖片名稱設置為保存的時間
Byte[] FileByte = new Byte[FileLength]; //圖象文件儲存到數組
Stream ObjectStream = UpFile.InputStream;//建立數據流對像,獲取一個 Stream 對象,該對象指向一個上載文件,以準備讀取該文件的內容。
ObjectStream.Read(FileByte, 0, FileLength); //讀取圖象文件數據
string StrSql = "Insert Into product(pro_name,img,title,content) Values('" + ptype + "',@ImageName,'" + title + "','" + content + "')";
OleDbCommand Cmd = new OleDbCommand(StrSql, MyConn);
//Cmd.Parameters.Add("@Image",OleDbType.Binary, FileLength).Value = FileByte;
Cmd.Parameters.Add("@ImageName", OleDbType.VarChar, 100).Value = ImageName;
MyConn.Open();
this.myFile.PostedFile.SaveAs(Server.MapPath("/upload") + "\\" + ImageName);
Cmd.ExecuteNonQuery();
MyConn.Close();
Response.Write("<script>alert('數據添加成功');window.location.href='Admin_Add.aspx?action="+ptype+"'</script>");
}
}
else
{
Response.Write("<script>alert('對不起,請選擇正確的的圖片!');window.location.href='Admin_Add.aspx?action=" + ptype + "'</script>");
return;
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
}
public static string FunStr(string str)
{
str = str.Replace("'", "‘");
str = str.Replace(" ", " ");
str = str.Trim();
if (str.Trim().ToString() == "")
str = "無";
return str;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -