how to download pdf on img button click in c#

Posted by Pavanpp under C# on 3/14/2013 | Points: 10 | Views : 3576 | Status : [Member] | Replies : 4
i want to download file when user click on download image button in the website.. Please help




Responses

Posted by: Santhi on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Try this code:
  protected void Button1_Click(object sender, EventArgs e)

{
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=filename.pdf");
Response.TransmitFile(Server.MapPath("~/Files/Text.Pdf"));
Response.End();
}


I have placed my pdf file in the Files folder

Thanks & Regards,
Santhi .V

Pavanpp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 3/14/2013 [Member] Starter | Points: 25

Up
0
Down

I have used the download button in the gridview
Use this handler

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Net;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {




SqlConnection con = con_manager.getcon();

string sql = "SELECT column_name FROM table " +
"WHERE condition";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@ScanID", SqlDbType.VarChar).Value = context.Request.QueryString["id"];


SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read()) //yup we found our image
{
context.Response.ContentType = dr["content_type"].ToString();
string filename = dr["file_name"].ToString();
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.BinaryWrite((byte[])dr["imagedata"]);

}
con.Close();

}

public bool IsReusable {
get {
return false;
}
}

}
and call that handler in the gridviewselected index changed event by passing the id
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string rowid;
rowid = GridView1.SelectedDataKey[0].ToString();
Response.Redirect("Download_Handler.ashx?id=" + rowid);
}

Pavanpp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 3/14/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Pavan,

I am attaching the App with database for the Requirement.Please check

 Download source file

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Pavanpp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Marrisa on: 8/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi
maybe you can open the file first and then you can download it easier. i've use this http://www.rasteredge.com/how-to/csharp-imaging/open-save-file/ :
public static void SaveDocumentFile(REDocument document, string filePath);
public static void SaveDocumentFile(BaseImage image, string filePath, BaseEncoder enc);
public static void SaveDocumentFile(List<BaseImage> images, string filePath, BaseEncoder enc);
public static void SaveDocumentFile(REDocument document, string filePath, BaseEncoder enc);

Pavanpp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response