随笔-18  评论-11  文章-12  trackbacks-0

需求:在一个人事管理系统中,所有的照片数据均已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 阅读(1456) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理