ASP.NET(C#) 實(shí)現(xiàn)將圖片以二進(jìn)制保存到數(shù)據(jù)庫(kù)中 轉(zhuǎn)
?<
??
?
?
default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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.OleDb;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
??? }
??? protected void Button1_Click(object sender, EventArgs e)
??? {
??????? try
??????? {
??????????? if (FileUpload1.HasFile)
??????????? {
??????????????? if (IsAllowedExtension(this.FileUpload1))
??????????????? {
??????????????????? //取得上傳文件的大小
??????????????????? int FileLen = FileUpload1.PostedFile.ContentLength;
??????????????????? Byte[] FileData = new Byte[FileLen];
??????????????????? //創(chuàng)建訪問(wèn)客戶端上傳文件的對(duì)象
??????????????????? HttpPostedFile hp = FileUpload1.PostedFile;
??????????????????? //創(chuàng)建數(shù)據(jù)流對(duì)象
??????????????????? Stream sr = hp.InputStream;
??????????????????? //將圖片數(shù)據(jù)放到FileData數(shù)組對(duì)象實(shí)例中,0代表數(shù)組指針的起始位置,FileLen代表指針的結(jié)束位置
??????????????????? sr.Read(FileData, 0, FileLen);
??????????????????? string strdatapath = "Provider = Microsoft.JET.OleDB.4.0;Data Source = " + Server.MapPath("mydata.mdb");
??????????????????? OleDbConnection conn = new OleDbConnection(strdatapath);
??????????????????? conn.Open();
??????????????????? OleDbCommand cmd = new OleDbCommand("insert into table_img(filename,img) values(filename,img)", conn);
??????????????????? //存儲(chǔ)過(guò)程插入到數(shù)據(jù)庫(kù)中
??????????????????? OleDbParameter para1 = new OleDbParameter("filename", OleDbType.VarChar);
??????????????????? para1.Value = "FileData";
??????????????????? cmd.Parameters.Add(para1);
??????????????????? //存儲(chǔ)過(guò)程插入到數(shù)據(jù)庫(kù)中
??????????????????? OleDbParameter para = new OleDbParameter("img", OleDbType.Binary);
??????????????????? para.Value = FileData;
??????????????????? cmd.Parameters.Add(para);
??????????????????? cmd.ExecuteNonQuery();
??????????????????? //彈出上傳成功的提示
??????????????????? Response.Write("");
????????????????? //? Response.Write("");
??????????????????? //關(guān)閉數(shù)據(jù)庫(kù)連接
??????????????????? conn.Close();
??????????????? }
??????????????? else
??????????????? {
??????????????????? Response.Write("");
????????????????? //? Response.Write("");
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("");
?????????????? // Response.Write("");
??????????? }
??????? }
??????? catch (Exception ex)
??????? {
????????????? Response.Write(ex.Message);
??????????? throw new Exception(ex.Message + "數(shù)據(jù)庫(kù)連接失敗");
??????? }
??? }
??? #region 實(shí)現(xiàn)真正意義上的文件類(lèi)型判斷
??? public static bool IsAllowedExtension(FileUpload hifile)
??? {
??????? System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
??????? System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
??????? string fileclass = "";
??????? byte buffer;
??????? try
??????? {
??????????? buffer = r.ReadByte();
??????????? fileclass = buffer.ToString();
??????????? buffer = r.ReadByte();
??????????? fileclass += buffer.ToString();
??????? }
??????? catch
??????? {
??????? }
??????? r.Close();
??????? fs.Close();
??????? if (fileclass == "255216" || fileclass == "7173")//說(shuō)明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
??????? {
??????????? return true;
??????? }
??????? else
??????? {
??????????? return false;
??????? }
??? }
??? #endregion
}
?
?
?
顯示圖片
?protected void Button2_Click(object sender, EventArgs e)
??? {
??????? // 在此處放置用戶代碼以初始化頁(yè)面
??????? show();? //顯示圖片
??? }
??? public void show()
??? {
??????? string strdatapath = "Provider = Microsoft.JET.OleDB.4.0;Data Source = " + Server.MapPath("mydata.mdb");
??????? OleDbConnection conn = new OleDbConnection(strdatapath);
?????? // conn.Open();
??????? string ss = "1";// Request.QueryString["id"].ToString();
??????? string s3 = "select * from table_img? where id=" + ss;
??????? OleDbCommand comm = new OleDbCommand(s3, conn);
??????? conn.Open();
??????? OleDbDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
??????? while (dr.Read())
??????? {
??????????? Response.Clear();
????????? //? Response.C;
??????????? Response.BinaryWrite((byte[])dr["img"]);//讀取
??????? }
??????? Response.End();
??????? conn.Close();
??? }