代码如下:
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand com = new SqlCommand("select top 1 photo from employees where photo is not null", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
//Response.ContentType = "application/octet-stream";
Response.ContentType = "image/jpeg";
byte[] sss = (byte[])dr["photo"];
Response.BinaryWrite((byte[])sss);
Image1.Attributes.Add("src","Default3.aspx");结果:
出现了一个文件下,在文件来的下级目录才会显示图片;Documents and Settings/hm_user_026/桌面/新資料夾/1.jpg;听说这二进制是压缩了,
如何求解;
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand com = new SqlCommand("select top 1 photo from employees where photo is not null", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
//Response.ContentType = "application/octet-stream";
Response.ContentType = "image/jpeg";
byte[] sss = (byte[])dr["photo"];
Response.BinaryWrite((byte[])sss);
Image1.Attributes.Add("src","Default3.aspx");结果:
出现了一个文件下,在文件来的下级目录才会显示图片;Documents and Settings/hm_user_026/桌面/新資料夾/1.jpg;听说这二进制是压缩了,
如何求解;
//photo.aspx.cs:
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand com = new SqlCommand("select top 1 photo from employees where photo is not null", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
Response.ClearContent();
Response.ContentType = "image/jpeg";
byte[] sss = (byte[])dr["photo"];
//close ...
Response.BinaryWrite((byte[])sss); //其它页引用如 aa.aspx:
//<html>....<img src="photo.aspx" />
下面这句画蛇添足:
Image1.Attributes.Add("src","Default3.aspx");因为你前面的代码是用来直接输出二进制流,你却又在这个aspx上弄了个image控件这个文件的aspx部分不要弄任何UI相关东西,只是在CS里写上输出代码.正确方法:
1.输出二进制图片的(也就是你写Response.BinaryWrite((byte[])sss)代码的)aspx保存为a.aspx
2.另建一个b.aspx,在这个b.aspx中
<img src="a.aspx" />
且不说别的,
a.aspx为什么会出default3?把你的a.aspx前台以及a.aspx.cs代码完整地放出来
System.IO.MemoryStream ms = new System.IO.MemoryStream();
...
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
Default3.aspx
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConn);
con.Open();
SqlCommand com = new SqlCommand("select top 1 photo from employees where photo is not null", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
Response.ClearContent();
Response.ContentType = "image/jpeg";
byte[] sss = (byte[])dr["photo"];
Response.BinaryWrite((byte[])sss); }
Default4.aspx <img src="Default3.aspx" alt="ss" />
以便得到一个正确的 mime 格式,
才能往浏览器推数据.如何推图像数据的代码太多了.
那么就应该检查其它地方,首先确保根本不去产生.zip文件(例如设置iis等的mime压缩范围仅仅限于aspx和html)。
{
ValidateCode img = new ValidateCode();//初始化验证码生成类
string valid = "";//定义随机数
MemoryStream ms = img.Create(out valid);//获取包括验证码图片的内存
context.Response.ClearContent();//清空输出流
//验证码存储在cookie中,供验证。
HttpCookie cookie = new HttpCookie("img", valid);
context.Response.Cookies.Add(cookie);
//context.Session["img"] = valid;//验证码存储在Session中,供验证。 context.Response.ContentType = "image/png";//输出流的格式
context.Response.BinaryWrite(ms.ToArray());//输出
context.Response.End();
}
在页面中直接让img的src属性等于这个一般处理程序的路径。
.ashx处理速度比.aspx快