我将数据库中的图像存储为二进制格式。点击链接按钮,我想下载该图像。帮我找一个合适的解决方案。谢谢。
代码:
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:
O / P屏幕: