How to open the download window when a dynamically created link is clicked in asp.net
- by Ranjana
i have stored the txtfile in the database.i need to show the txtfile when i clik the link. and this link has to be created dynamically.
my code below:
aspx code:
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataTable dtassignment = new DataTable();
dtassignment = serviceobj.DisplayAssignment(Session["staffname"].ToString());
if (dtassignment != null)
{
Byte[] bytes = (Byte[])dtassignment.Rows[0]["Data"];
//download(dtassignment);
}
divlink.InnerHtml = "";
divlink.Visible = true;
foreach (DataRow r in dtassignment.Rows)
{
divlink.InnerHtml += "<a href='" +
"'onclick='download(dtassignment)'>" +
r["Filename"].ToString() + "</a>" + "<br/>";
}
}
}
-
public void download(DataTable dtassignment)
{
System.Diagnostics.Debugger.Break();
Byte[] bytes = (Byte[])dtassignment.Rows[0]["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dtassignment.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename="
+ dtassignment.Rows[0]["FileName"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
i have got the link dynamically, but i did not able to download the txtfile when i clik the link. how to carry out this. pls help me out...