protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Stream mystream = FileUpload1.PostedFile.InputStream;//FileUpload1是上传控件..
int length = FileUpload1.PostedFile.ContentLength;
byte[] bytes = new byte[length];
mystream.Read(bytes, 0, length);
mystream.Close();
string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
string str="insert into image (img) values('" + bytes + "')";
SqlCommand mycomm = new SqlCommand(str, myconn);
mycomm.ExecuteNonQuery();
myconn.Close();
}
}
上面把图片存入数据库...
下面是把图片从数据库中读出来...
protected void Button2_Click(object sender, EventArgs e)
{
string imgtype = FileUpload1.PostedFile.ContentType;
string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
string str="select img from image where ID=5";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
//SqlDataAdapter myda = new SqlDataAdapter(str, conn);
//DataSet myds =new DataSet();
//myda.Fill(myds);
SqlCommand mycom = new SqlCommand(str, myconn);
SqlDataReader mydr = mycom.ExecuteReader();
if (mydr.Read())
{
Response.ContentType = imgtype;
Response.BinaryWrite((byte[])mydr["img"]);
}
else
{
Response.Write("没有从数据库中读取图片");
}
myconn.Close();
}
为什么我的页面上只显示System.Byte[]
{
if (FileUpload1.HasFile)
{
Stream mystream = FileUpload1.PostedFile.InputStream;//FileUpload1是上传控件..
int length = FileUpload1.PostedFile.ContentLength;
byte[] bytes = new byte[length];
mystream.Read(bytes, 0, length);
mystream.Close();
string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
string str="insert into image (img) values('" + bytes + "')";
SqlCommand mycomm = new SqlCommand(str, myconn);
mycomm.ExecuteNonQuery();
myconn.Close();
}
}
上面把图片存入数据库...
下面是把图片从数据库中读出来...
protected void Button2_Click(object sender, EventArgs e)
{
string imgtype = FileUpload1.PostedFile.ContentType;
string conn = "server=(local);database=Image;Uid=sa;Pwd=123456";
string str="select img from image where ID=5";
SqlConnection myconn = new SqlConnection(conn);
myconn.Open();
//SqlDataAdapter myda = new SqlDataAdapter(str, conn);
//DataSet myds =new DataSet();
//myda.Fill(myds);
SqlCommand mycom = new SqlCommand(str, myconn);
SqlDataReader mydr = mycom.ExecuteReader();
if (mydr.Read())
{
Response.ContentType = imgtype;
Response.BinaryWrite((byte[])mydr["img"]);
}
else
{
Response.Write("没有从数据库中读取图片");
}
myconn.Close();
}
为什么我的页面上只显示System.Byte[]
解决方案 »
- MSchart控件 怎么设置右侧的Y轴
- webChart生成图片
- asp.net 注册提示密码 强 中 弱
- 高手救命:gridview编码模式下列超长超大,列是动态增加的
- asp.net 中如何合并单元格?
- AspNetPager的翻页事件老丢失涅??
- 新闻小偷
- 为什么不能选中一行,并将其删除呢?
- 大家帮帮忙,哪里有《ASP.NET入门经典—C#编程篇》的电子版下载?
- Dev ASPxGridView的几个问题
- 如何保存gridview的值 我的gridview的数据源自定义的datatable,没有从数据库里取值,在gridview里有textbox,希望刷新页面后textbox里的值
- 求《征服ASP.NET 2.0 Ajax__Web开发技术详解》源代码(全套)
string imgtype = FileUpload1.PostedFile.ContentType; LZ这个地方的FileUpload1中还有值么,如果没有值,你如何能取的到其图片类型呢.
一般来说,我们都是在把图片二进制流写入数据库的同时把图片的格式也同时写入数据库
到时候直取读取.
Response.BinaryWrite(b);