protected void BtnSubmit_Click(object sender, EventArgs e) { { try { MyConn = DB.CreateDB();
string title =FunStr(this.TxtTitle.Text);//轉(zhuǎn)換 string content =FunStr(this.TxtContent.Text); string ptype = Request.QueryString["action"].ToString(); string FileName = myFile.Value; HttpPostedFile UpFile = myFile.PostedFile; //獲取對由客戶端指定的上傳文件的訪問 FileLength = UpFile.ContentLength;//獲取上傳文件的字節(jié)大小 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('數(shù)據(jù)添加成功');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 > 1024000)//判斷圖片是否大于200k(根據(jù)自己的需要判斷大?。?BR> { Response.Write("<script>alert('對不起,圖片大小不能大于1M');window.location.href='Admin_Add.aspx?action=" + ptype + "'</script>"); return; } else { string ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + exName;//圖片名稱設(shè)置為保存的時(shí)間 Byte[] FileByte = new Byte[FileLength]; //圖象文件儲(chǔ)存到數(shù)組 Stream ObjectStream = UpFile.InputStream;//建立數(shù)據(jù)流對像,獲取一個(gè) Stream 對象,該對象指向一個(gè)上載文件,以準(zhǔn)備讀取該文件的內(nèi)容。
ObjectStream.Read(FileByte, 0, FileLength); //讀取圖象文件數(shù)據(jù) 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('數(shù)據(jù)添加成功');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>"); } }
}
|