How to open the download window when a dynamically created link is clicked in asp.net

Posted by Ranjana on Stack Overflow See other posts from Stack Overflow or by Ranjana
Published on 2010-04-27T06:22:51Z Indexed on 2010/04/27 6:33 UTC
Read the original article Hit count: 264

Filed under:
|
|
|
|

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...

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#