I have a page with an image button. When the user clicks on the image button, the server creates a PDF of the page and a Response with a download is sent back to the client. The problem is that this can take some time. How can/should I handle the waiting for the client?

My initial thought was to do a onClientClick event on the button that shows a loader GIF. But I am not able to detect when the response is finished so that I can hide it again. I have tried using *RegisterStartupScript* as well as a *Response.Write([script])*, but it all seems to get overriden by the PDF's *Response.BinaryWrite(buffer)* - the file gets downloaded but nothing else happens.

Here's my code with just the download logic:

```
protected void imgPrint_Click(object sender, ImageClickEventArgs e)
        {
            PDF pdf = new PDF();

            byte[] buffer = pdf.ExportToPDF(Request.QueryString["id"].ToString(), Request.QueryString["mode"].ToString());

            string att = "attachment; filename=" + Request.QueryString["mode"].ToString() + ".pdf";
            Response.ClearContent();
            Response.AddHeader("content-disposition", att);
            Response.ContentType = "application/pdf";
            Response.ContentEncoding = Encoding.Default;
            Response.BinaryWrite(buffer);
        }

```

And the image button with an onClientClick event that currently only shows an alert:

```
<asp:ImageButton ID="imgPrint" runat="server" 
                              ImageUrl="~/bilder/skriv_ut.png" Width="45" Height="36" 
                              onclick="imgPrint_Click" OnClientClick="loader(true);" />

```