asp.net(C#)与ACCESS数据库存取二进制图片
前台代码:<asp:FileUploadID="UP_FILE"runat="server"Width="300px"/>名字:<asp:TextBoxID="TextB...
前台代码:
<asp:FileUpload ID="UP_FILE" runat="server" Width="300px"/>名字:<asp:TextBox ID="TextBox1" runat="server" MaxLength="20"></asp:TextBox><asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="height: 26px" Text="Button" />
...............................
后台代码:
protected Int32 FileLength = 0; //记录文件长度变量
protected void Button1_Click(object sender, EventArgs e)
{
string name = UP_FILE.PostedFile.FileName;
string type = name.Substring(name.LastIndexOf(".") + 1);
HttpPostedFile upFile = UP_FILE.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
int fileLength = upFile.ContentLength;//记录文件的长度
byte[] FileByteArray = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
fileStream.Read(FileByteArray, 0, fileLength);
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png" || type == "JPG" || type == "GIF" || type == "BMP" || type == "PNG")
{
OleDbConnection Con = OledbCon.OledbAccess.OledConeadata();//定义数据库连接对象
String SqlCmd = "INSERT INTO imgdata([user], [imagedata]) VALUES('" + this.TextBox1.Text.Trim() + "', '" + FileByteArray + "')";
Con.Open();//打开数据库
OleDbCommand CmdObj = new OleDbCommand(SqlCmd, Con);//定义Command对象
CmdObj.ExecuteNonQuery();//执行数据库插入操作
Con.Close();//关闭数据库连接
//提示上传成功
Response.Write("<script language=javascript>alert('成功上传图片!')</script>");
}
}
}
}
..............................
读取二进制图片代码:
byte[] b_logoImg;
protected void Page_Load(object sender, EventArgs e)
{
string strSQL = "SELECT top 1 * FROM imgdata order by id desc";//只要一张图片
OleDbConnection conn = OledbCon.OledbAccess.OledConeadata();//连接数据库
conn.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "imgdata");
conn.Close();
b_logoImg = (byte[])(ds.Tables[0].Rows[0][2]);//得到数据库二进制字段内容
if (b_logoImg.Length > 0)
{
//MemoryStream ms = new MemoryStream(b_logoImg);
//Response.Clear();
//Response.ContentType = "image/gif";
//Response.OutputStream.Write(b_logoImg, 0, b_logoImg.Length);
Response.BinaryWrite(b_logoImg);
Response.End();
}
}
...........................
问题:能成功的以二进制上传到数据库(用的是ACCESS数据库),但是读取图片的时候就是显示不出图片。怎么回事,请高手帮忙。。。小弟在这里谢谢了(成功后加分)
Button1_Click事件完成后,ACCESS数据库中是否有值。
页面加载的时候加个判断,if(!IsPostBack)再执行读取图片信息。
再就是看下执行完b_logoImg = (byte[])(ds.Tables[0].Rows[0][2])这句后,b_logoImg里的值,是不是有读取到相应的二进制文件流的值
这些都正确,所以才难到我了,能解决的帮帮忙,感激不尽 展开
<asp:FileUpload ID="UP_FILE" runat="server" Width="300px"/>名字:<asp:TextBox ID="TextBox1" runat="server" MaxLength="20"></asp:TextBox><asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="height: 26px" Text="Button" />
...............................
后台代码:
protected Int32 FileLength = 0; //记录文件长度变量
protected void Button1_Click(object sender, EventArgs e)
{
string name = UP_FILE.PostedFile.FileName;
string type = name.Substring(name.LastIndexOf(".") + 1);
HttpPostedFile upFile = UP_FILE.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
int fileLength = upFile.ContentLength;//记录文件的长度
byte[] FileByteArray = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
fileStream.Read(FileByteArray, 0, fileLength);
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png" || type == "JPG" || type == "GIF" || type == "BMP" || type == "PNG")
{
OleDbConnection Con = OledbCon.OledbAccess.OledConeadata();//定义数据库连接对象
String SqlCmd = "INSERT INTO imgdata([user], [imagedata]) VALUES('" + this.TextBox1.Text.Trim() + "', '" + FileByteArray + "')";
Con.Open();//打开数据库
OleDbCommand CmdObj = new OleDbCommand(SqlCmd, Con);//定义Command对象
CmdObj.ExecuteNonQuery();//执行数据库插入操作
Con.Close();//关闭数据库连接
//提示上传成功
Response.Write("<script language=javascript>alert('成功上传图片!')</script>");
}
}
}
}
..............................
读取二进制图片代码:
byte[] b_logoImg;
protected void Page_Load(object sender, EventArgs e)
{
string strSQL = "SELECT top 1 * FROM imgdata order by id desc";//只要一张图片
OleDbConnection conn = OledbCon.OledbAccess.OledConeadata();//连接数据库
conn.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "imgdata");
conn.Close();
b_logoImg = (byte[])(ds.Tables[0].Rows[0][2]);//得到数据库二进制字段内容
if (b_logoImg.Length > 0)
{
//MemoryStream ms = new MemoryStream(b_logoImg);
//Response.Clear();
//Response.ContentType = "image/gif";
//Response.OutputStream.Write(b_logoImg, 0, b_logoImg.Length);
Response.BinaryWrite(b_logoImg);
Response.End();
}
}
...........................
问题:能成功的以二进制上传到数据库(用的是ACCESS数据库),但是读取图片的时候就是显示不出图片。怎么回事,请高手帮忙。。。小弟在这里谢谢了(成功后加分)
Button1_Click事件完成后,ACCESS数据库中是否有值。
页面加载的时候加个判断,if(!IsPostBack)再执行读取图片信息。
再就是看下执行完b_logoImg = (byte[])(ds.Tables[0].Rows[0][2])这句后,b_logoImg里的值,是不是有读取到相应的二进制文件流的值
这些都正确,所以才难到我了,能解决的帮帮忙,感激不尽 展开
2个回答
展开全部
<%@ language = "vbscript" %>
<%
dim sn
dim constr
dim sqlstr
***************************************************
Response.Expires =-1
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"
***************************************************
'sn = request.QueryString("sn")
sn=trim(Request("sn"))
none=""
if sn = "" then
sn=0
none=" or 1=0"
end if
set connGraph = server.CreateObject("ADODB.connection")
constr="Provider=sqloledb;Server=DTLTJFB\DTSQL2000;initial Catalog=dt_unicom;UID=identify_admin;PWD=sa;"
connGraph.Open constr
sqlstr="select img_data from [aa_identify_detail] where id=" & sn & none
set rec=connGraph.Execute(sqlstr)
if rec.eof or rec.bof then Response.end
img=rec("img_data")
isize=len(img)*2+500
'response.write isize
***************************************************
Response.ContentType = "image/jpeg"
Response.BinaryWrite rec("img_data").getChunk(isize)
***************************************************
set rec=nothing
connGraph.close
set connGraph=nothing
%>
以前做的一个程序。****号处,好好看看。
<%
dim sn
dim constr
dim sqlstr
***************************************************
Response.Expires =-1
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"
***************************************************
'sn = request.QueryString("sn")
sn=trim(Request("sn"))
none=""
if sn = "" then
sn=0
none=" or 1=0"
end if
set connGraph = server.CreateObject("ADODB.connection")
constr="Provider=sqloledb;Server=DTLTJFB\DTSQL2000;initial Catalog=dt_unicom;UID=identify_admin;PWD=sa;"
connGraph.Open constr
sqlstr="select img_data from [aa_identify_detail] where id=" & sn & none
set rec=connGraph.Execute(sqlstr)
if rec.eof or rec.bof then Response.end
img=rec("img_data")
isize=len(img)*2+500
'response.write isize
***************************************************
Response.ContentType = "image/jpeg"
Response.BinaryWrite rec("img_data").getChunk(isize)
***************************************************
set rec=nothing
connGraph.close
set connGraph=nothing
%>
以前做的一个程序。****号处,好好看看。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询