ASP.NET(C#) 实现将图片以二进制保存到数据库中 转
扫描二维码
随时随地手机看文章
<
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];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpload1.PostedFile;
//创建数据流对象
Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
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);
//存储过程插入到数据库中
OleDbParameter para1 = new OleDbParameter("filename", OleDbType.VarChar);
para1.Value = "FileData";
cmd.Parameters.Add(para1);
//存储过程插入到数据库中
OleDbParameter para = new OleDbParameter("img", OleDbType.Binary);
para.Value = FileData;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
//弹出上传成功的提示
Response.Write("");
// Response.Write("");
//关闭数据库连接
conn.Close();
}
else
{
Response.Write("");
// Response.Write("");
}
}
else
{
Response.Write("");
// Response.Write("");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
throw new Exception(ex.Message + "数据库连接失败");
}
}
#region 实现真正意义上的文件类型判断
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")//说明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)
{
// 在此处放置用户代码以初始化页面
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();
}