access数据库用二进制储存图片,并在web页面显示

储存:传入确定图片的唯一序列号id 和图片的保存路径filePath。
private bool AddImg(string id,string filePath)
{
           try
           {
             FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
             BinaryReader br = new BinaryReader(fs);
             byte[] photo = br.ReadBytes((int)fs.Length);
             br.Close();
             fs.Close();

             OleDbConnection cn = new OleDbConnection(strConn);
             OleDbCommand cmd = new OleDbCommand("insert into F_TelImg(id, val) VALUES (@ID, @Img)", cn);

             cmd.Parameters.Add("@ID", OleDbType.VarChar).Value = id;
             cmd.Parameters.Add("@Img", OleDbType.Binary, photo.Length).Value = photo;

             cn.Open();
             cmd.ExecuteNonQuery();
             cn.Close();
           }
           catch()
           {
             return false;
           }
           return true; ;
}

显示:需要创建独立的显示图片的aspx文件,下面函数是该页面的页面导入事件。通过img.aspx?id=xxx 传入参数来显示对应id的图片。srxljl
protected void Page_Load(object sender, EventArgs e)
{
       if (Request["id"] != null && Request["id"] != "")
       {
         // table.GetDataSet()是封装好的读取数据库数据的函数srxljl
         DataSet ds = table.GetDataSet("Img", "select val from Img where id='" + Request["id"] + "'");
         while (ds!=null)
         {
           Response.Clear();
           Response.ContentType="image/jpeg"; // 确定图片显示的格式srxljl
           Response.BinaryWrite((byte[])ds.Tables["Img"].Rows[0]["val"]);
           Response.End();
         }
       }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值