Thursday, 15 November 2012

Save Dialog Box on Button/LinkButton Click Event

Save Dialog Box on Button/LinkButton Click Event


There are times when we get into this situation when we have to give an option to the User to save the associated Pdf/Word file to his system.

Here is how we can do this:

Suppose We want to display the save option for XYZ.pdf on the click of the link button.

Design Part:


<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Descargar Catalogos de Recambios</asp:LinkButton>

Code Behind:


    protected void LinkButton1_Click(object sender, EventArgs e)

    {

        FileInfo fileInfo = new FileInfo(Server.MapPath("XYZ.pdf"));

        Response.Clear();

        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);

        Response.AddHeader("Content-Length", fileInfo.Length.ToString());

        Response.ContentType = "application/octet-stream";

        Response.Flush();

        Response.WriteFile(fileInfo.FullName);

        Response.End();

    }

No comments:

Post a comment

Thank You for Your Comments. We will get back to you soon.

back to top