如何从数据库下载二进制图像?

时间:2014-11-13 06:09:34

标签: c# asp.net image binary

我将数据库中的图像存储为二进制格式。点击链接按钮,我想下载该图像。帮我找一个合适的解决方案。谢谢。

代码:

 protected void Page_Load(object sender, EventArgs e)
  {
   if (!IsPostBack)
     {

      pdfDataSetTableAdapters.tbl_imgTableAdapter td;
      td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
      DataTable dt = new DataTable();
      dt = td.GetId();        //Sql1
      DropDownList1.DataSource = dt;
      DropDownList1.DataTextField = "Id";
      DropDownList1.DataValueField = "Id";
      DropDownList1.DataBind();
      DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Id--", ""));
     }

  }
  protected void Button1_Click(object sender, EventArgs e)
   {
     pdfDataSetTableAdapters.tbl_imgTableAdapter td;
     td = new pdfDataSetTableAdapters.tbl_imgTableAdapter();
     DataTable dt = new DataTable();
     dt = td.GetImg(int.Parse(DropDownList1.SelectedValue));   //Sql2
     foreach (DataRow row in dt.Rows)
      {
         byte[] img2 = (byte[])row["img"];
         string base2 = Convert.ToBase64String(img2);
         Image1.ImageUrl = "data:image/jpg;base64," + base2;
       }     
   }

protected void LinkButton1_Click(object sender, EventArgs e)
   {
      // image download
   }

SQL1:

SELECT Id FROM tbl_img

SQL2:

SELECT img FROM tbl_img WHERE (Id = @Id)

ASPX:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"> </asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show" />
<asp:LinkButton ID="LinkButton1" runat="server">Download</asp:LinkButton>
<asp:Image ID="Image1" runat="server" />

DB:

enter image description here enter image description here

O / P屏幕:

enter image description here

1 个答案:

答案 0 :(得分:1)

现在时间较短,我建议你一个链接。按照其中提到的步骤操作。它一定会帮到你。

Downloading files doc