如何使用链接按钮下载图片? [英] How to download image using link button?
问题描述
我正在尝试使用链接按钮下载图片。任何人都知道如何使用链接按钮下载图像。帮我找到一个正确的解决方案。谢谢。
代码:
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(dt.Rows中的DataRow行)
{
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))
{
//检查文件是否为图像
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 =DropDownList1runat =serverAutoPostBack =True> < / ASP:DropDownList的>
< asp:Button ID =Button1runat =serverOnClick =Button1_ClickText =Show/>
< asp:LinkButton ID =LinkButton1runat =server>下载< / asp:LinkButton>
< asp:Image ID =Image1runat =server/>
SQL1:
code> SELECT Id FROM tbl_img
SQL2:
SELECT img FROM tbl_img WHERE(Id = @Id)
DB:
O / P屏幕:
以下是实际输出。首先我们要从下拉列表中选择id,然后点击show按钮,然后在页面上显示适当的图像。我想通过点击下载链接按钮下载该图像。帮我找一个解决方案。谢谢。
错误:
将以下代码添加到链接按钮单击事件。
代码
string sFile = Image1.ImageUrl;
//确保提供文件链接
if(string.IsNullOrEmpty(sFile))
{
return;
}
//确保文件存在
FileInfo fi = new FileInfo(Server.MapPath(sFile));
if(!fi.Exists)
{
return;
}
if(!string.IsNullOrEmpty(sFile))
{
//检查文件是否为图像
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);
}
添加以下命名空间
以及
使用System.IO;
使用System.Collections;
使用System.Collections.Specialized;
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:
Add the following code to your Link Button Click event.
Code
string sFile = Image1.ImageUrl;
// make sure file link is provided
if (string.IsNullOrEmpty(sFile))
{
return;
}
// make sure file exists
FileInfo fi = new FileInfo(Server.MapPath(sFile));
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);
}
Add the following namespaces
as well.
using System.IO;
using System.Collections;
using System.Collections.Specialized;
这篇关于如何使用链接按钮下载图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!