Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

c# - How to download image using link button?

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:

enter image description here

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.

enter image description here

ERROR:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The new and fancy way is the HTML5 download attribute. It has limited support, but I hope it will change: http://caniuse.com/#search=download

Otherwise, you have to create a postback event handler, and print the raw image with the Content-Disposition: attachment header. If the user clicks the link, it should do the postback, and then the browser will offer to save the file, instead of displaying it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...