代码如下:
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string strsql = "select * from [File]";
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.ContentType = dr["PicType"].ToString();
Response.BinaryWrite((byte[])dr["Pic"]);
}
conn.Close();
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string strsql = "select * from [File]";
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Response.ContentType = dr["PicType"].ToString();
Response.BinaryWrite((byte[])dr["Pic"]);
}
conn.Close();
试下
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((byte[])dr["Pic"]);
Response.End();
请教楼主,这样子循环的作用是什么?
conn.Open();
string strsql = "select * from [File]";
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read()) //只读取第一行
{
Response.ContentType = dr["PicType"].ToString(); //这里要保证是"image/jpg"的格式;
Response.BinaryWrite((byte[])dr["Pic"]);
}else
{
Response.Write("没有任何图片");//帮你检查一下是否取到值
}
conn.Close();
Response.End();
因为页面也是文件,就好像同一张图片不能同时显示另外一张图片是一个道理如果想显示多张图片请使用下面的方式<img src="url+图片1-id参数"/>
<img src="url+图片2-id参数"/>
在src中通过url+?image=id的方式传递参数然后显示图片的页面通过参数得到对应的图片显示一张图片但是请求页面因为有多个img所以可以显示多张图片