我正在尝试使用链接按钮下载图像。任何人都知道如何使用链接按钮下载图像。帮我找一个合适的解决方案。谢谢。
代码:
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屏幕: 以下是实际输出。首先我们必须从下拉列表中选择id,然后点击show按钮,然后在页面上显示相应的图像。我想通过单击下载链接按钮下载该图像。帮我找一个解决方案。谢谢。
ERROR:
答案 0 :(得分:0)
新的和奇特的方式是HTML5下载属性。它的支持有限,但我希望它会改变:http://caniuse.com/#search=download
否则,您必须创建一个回发事件处理程序,并使用Content-Disposition:attachment标头打印原始图像。如果用户点击该链接,它应该进行回发,然后浏览器将提供保存文件,而不是显示它。
答案 1 :(得分:0)
将以下代码添加到Link Button Click事件中。
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);
}
同时添加以下namespaces
。
using System.IO;
using System.Collections;
using System.Collections.Specialized;