Update page after file download

Viewed 33279

I put together a download script after some wonderful help from stack overflow the other day. However I have now found that after the file has been downloaded I need to reload the page to get rid of the progress template on the aspx page. The code to remove the template worked before I added in the download code.

Code to remove progress template: upFinanceMasterScreen.Update();

I've tried calling putting this before and after the redirect to the IHttpHandler

Response.Redirect("Download.ashx?ReportName=" + "RequestingTPNLeagueTable.pdf");


public class Download : IHttpHandler {

public void ProcessRequest(HttpContext context)
{    

   StringBuilder sbSavePath = new StringBuilder();
   sbSavePath.Append(DateTime.Now.Day);
   sbSavePath.Append("-");
   sbSavePath.Append(DateTime.Now.Month);
   sbSavePath.Append("-");
   sbSavePath.Append(DateTime.Now.Year);

    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpResponse objResponce = context.Response;
    String test = HttpContext.Current.Request.QueryString["ReportName"];
    HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + test);
    objResponce.WriteFile(context.Server.MapPath(@"Reports\" + sbSavePath + @"\" + test));    

}
 public bool IsReusable { get { return true; } } 

Thanks for any help you can provide!

4 Answers

This is quick and easy to hack if needed.

Step 1: Add hidden button to .aspx page:

<asp:Button ID="btnExportUploaded" runat="server" Text="Button" style="visibility:hidden"  OnClick="btnExportUploaded_Click" CssClass="btnExportUploaded" />

Step 2: Perform your default postback action and at the end register a startup script with jquery call which will trigger the hidden button click and cause a file to download:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "modalstuff", "$('.btnExportUploaded').click();", true);

A simpler approach is to just do whatever needed in the PostBack event, and register a reload script with an additional argument to indicate the download. Something like:

C# code:

protected void SaveDownloadCount(int downloadId)
{
    // Run in a PostBack event. 
    // 1) Register download count, refresh page, etc.
    // 2) Register a script to reload the page with an additional parameter to indicate the download. 
    Page.ClientScript.RegisterStartupScript(GetType(), "download",
        "$(document).ready(function(){window.location.href = window.location.pathname + window.location.search ? '&' : '?' + 'printId={0}';});".Replace("{0}", downloadId.ToString()), true);
}

Then, in PageLoad we need to check for the download paramenter and serve the file:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int printId;
            if (Request.QueryString["printId"] != null && int.TryParse(Request.QueryString["printId"], out printId))
            {
                // Check if the argument is valid and serve the file. 
            }
            else
            {
                // Regular initialization
            }
        }
    }

This is simalar to @puddleglum answer but without the drawback of the "out of synch" timeout.

Related