下载二进制流图片

 //下载按钮
    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);
        string  JobDetailID = (((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;
            //Open(downimage);
            //string fileName = "123.jpg";//客户端保存的文件名
            //string filePath = Server.MapPath("images/1.jpg");//路径
            //以字符流的形式下载文件
            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();


        }
    }

   public void UploadFile(byte[] fs, string FileName)
    {
        try
        {
            MemoryStream m = new MemoryStream(fs);//定义并实例化一个内存流,来存放上传的图片二进制流
            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 = null;
            m = null;
            //InsertErrorLog("图片上传成功");
        }
        catch (Exception ex)
        {

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

        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值