需求:在一個(gè)人事管理系統(tǒng)中,所有的照片數(shù)據(jù)均已image形式保存在數(shù)據(jù)庫(kù)。使用image控件在員工個(gè)人資料中顯示照片。
首先建一個(gè)顯示圖片的文件photo.aspx,其中photo.aspx.cs文件這樣寫:
private void Page_Load(object sender, System.EventArgs e)
{
string hrid = Request.QueryString["hrid"];
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
con.Open();
String select = "Select photo from hrsdb.dbo.EplPersonnelArchives where hrid = " + hrid;
DataSet ds = new DataSet();
byte[] photo = new byte[0];
SqlDataAdapter da = new SqlDataAdapter(select,con);
con.Close();
da.Fill(ds);
DataRow dr;
dr = ds.Tables[0].Rows[0];
if(dr["photo"].ToString() != "")
photo = (byte[])dr["photo"];
Response.ContentType = "image/jpeg";
Response.BinaryWrite(photo);
}
在個(gè)人資料頁(yè)面調(diào)用這個(gè)頁(yè)面this.Image1.ImageUrl = "photo.aspx?hrid=" + hRID;
注意:
1. Response.ContentType 為image/jpeg,而不是網(wǎng)上資料中的jpg。不同的ContentType 會(huì)影響客戶端所看到的效果.默認(rèn)的ContentType為 text/html 也就是網(wǎng)頁(yè)格式.application/octet-stream則是以下載文件形式。http://www.7747.net/kf/200801/23224.html
2. 使用Response.BinaryWrite()方法,而不是Response.Write()方法。BinaryWrite 方法在不進(jìn)行字符轉(zhuǎn)換的情況下直接向輸出寫數(shù)據(jù)。http://www.w3school.com.cn/asp/met_binarywrite.asp
類別:asp.net 查看評(píng)論文章來(lái)源:
http://hi.baidu.com/hawkingliu/blog/item/8c21b97e0cdde63d0cd7daf1.html
posted on 2008-05-29 17:08
ronliu 閱讀(1495)
評(píng)論(0) 編輯 收藏 引用