二进制显示和下载图片

1新建页面

例如:Default1.aspx    在页面上添加图片标签

  <asp:Image ID="Image1" runat="server" />

添加一个查看按钮

  //查看按钮
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;

        int id = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label3")).Text);
        int JobDetailID = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label4")).Text);
        Checkkind = Request["Checkkind"].ToString();
        pic.Style.Add("display", "block");

        //传值给Default页面
        Image1.ImageUrl = "Default.aspx?id=" + id + "&checkkind=" + Checkkind + "&JobDetailID=" + JobDetailID;
     

    }

 

Default.aspx页面接收

 protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        string checkkind = Request.QueryString["checkkind"];
        string JobDetailID = Request.QueryString["JobDetailID"];

        SqlParameter[] para = {
                              new SqlParameter("@Idkey",id),
                              new SqlParameter("@CheckKind",checkkind),
                               new SqlParameter("@JobDetailID",JobDetailID)
                              };
        string sProcName = "P_shiguImage_getData_ByIdkeyandCheckKind";

        DataSet ds = new DataSet();
        ds = SQLHelper.SqlHelper.ExecuteDataset(new GFunc().GetConnStr(), CommandType.StoredProcedure, sProcName, para);
        if (ds.Tables[0].Rows.Count > 0)
        {

            Response.BinaryWrite((byte[])(ds.Tables[0].Rows[0]["piccontent"]));

            Response.End();
        }
    }

 

下载按钮 :Default1.aspx   

  //下载按钮
    protected void LinkButton2_Click(object sender, EventArgs e)
    {

        int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;

        int id = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label3")).Text);
        int JobDetailID = int.Parse(((Label)this.GridView1.Rows[row].FindControl("Label4")).Text);
        Checkkind = Request["Checkkind"].ToString();
        DataSet ds = new DataSet();
        SqlParameter[] para = {
                              new SqlParameter("@Idkey",id),
                              new SqlParameter("@CheckKind",Checkkind),
                               new SqlParameter("@JobDetailID",JobDetailID)
                              };
        string sProcName = "P_shiguImage_getData_ByIdkeyandCheckKind";
        ds = SQLHelper.SqlHelper.ExecuteDataset(new GFunc().GetConnStr(), CommandType.StoredProcedure, sProcName, para);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string FileName = GetValueByDS(ds, "picname");
         
            byte[] piccontent =(byte[])(ds.Tables[0].Rows[0]["piccontent"]);


            try
            {
                MemoryStream m = new MemoryStream(piccontent);//定义并实例化一个内存流,来存放上传的图片二进制流
                if (!File.Exists(Server.MapPath(".") + "\\images1"))
                {
                    Directory.CreateDirectory(Server.MapPath(".") + "\\images1");
                }
                FileStream f = new FileStream(Server.MapPath(".") + "\\images1\\" + FileName, FileMode.Create);//把内存里的文件写入文件流
              
                m.WriteTo(f);
                m.Close();
                f.Close();
                f = null;
                m = null;
                //InsertErrorLog("图片上传成功");
              
            }
            catch (Exception ex)
            {

                Response.Write(ex.Message);
                Response.Write("图片上传失败");

            }
            string downimage = Server.MapPath(".") + "\\images1\\" + FileName;
          
            //以字符流的形式下载文件
            FileStream fs = new FileStream(downimage, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

          
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值