需求:在一個人事管理系統中,所有的照片數據均已image形式保存在數據庫。使用image控件在員工個人資料中顯示照片。
首先建一個顯示圖片的文件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);
}
在個人資料頁面調用這個頁面this.Image1.ImageUrl = "photo.aspx?hrid=" + hRID;
注意:
1. Response.ContentType 為image/jpeg,而不是網上資料中的jpg。不同的ContentType 會影響客戶端所看到的效果.默認的ContentType為 text/html 也就是網頁格式.application/octet-stream則是以下載文件形式。http://www.7747.net/kf/200801/23224.html
2. 使用Response.BinaryWrite()方法,而不是Response.Write()方法。BinaryWrite 方法在不進行字符轉換的情況下直接向輸出寫數據。http://www.w3school.com.cn/asp/met_binarywrite.asp
類別:asp.net 查看評論文章來源:
http://hi.baidu.com/hawkingliu/blog/item/8c21b97e0cdde63d0cd7daf1.html
posted on 2008-05-29 17:08
ronliu 閱讀(1500)
評論(0) 編輯 收藏 引用