I am trying to download image using link button. Any one know how to download a image using link button. Help me to find a proper solution. Thank you.
Code:
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();
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));
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)
{
string sFile = Image1.ImageUrl;
if (string.IsNullOrEmpty(sFile))
{
return;
}
FileInfo fi = new FileInfo(Server.MapPath(sFile)); // error popup here
if (!fi.Exists)
{
return;
}
if (!string.IsNullOrEmpty(sFile))
{
// check if the file is an image
NameValueCollection imageExtensions = new NameValueCollection();
imageExtensions.Add(".jpg", "image/jpeg");
imageExtensions.Add(".gif", "image/gif");
imageExtensions.Add(".png", "image/png");
if (imageExtensions.AllKeys.Contains(fi.Extension))
{
Response.ContentType = imageExtensions.Get(fi.Extension);
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
Response.TransmitFile(fi.FullName);
Response.End();
}
Response.Redirect(sFile);
}
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" />
SQL1:
SELECT Id FROM tbl_img
SQL2:
SELECT img FROM tbl_img WHERE (Id = @Id)
DB:
O/P Screen:
Given below is the actual out put. first we have to select id from drop down list, after that click on show button then it display appropriate image on page. I want to download that image by clicking the download link button. Help me to find a solution. Thank you.
ERROR:
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…