Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update page after file download

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!

like image 382
flyersun Avatar asked Oct 26 '10 15:10

flyersun


People also ask

How do I reload the page after a download?

You can't reload the page after a download and there is no way of knowing when the download completes or if the file is downloaded at all. You should instigate the download in a new tab, so if your download is a link then add target="_blank"

How to instigate a download from a page?

You should instigate the download in a new tab, so if your download is a link then add target="_blank" You could then reload the page on the click of that link. You haven't explained how you instigate the download so it's hard to give a specific solution. The file is downloaded on a button click.

How to serve a download from page 1 to page 2?

Page 1 links to page 2. Page 2 serves a download using the following code: header ("Content-disposition: attachment; filename= '$filename'"); header ('Content-type: application/pdf'); readfile ($file); header ("location: mainpage.php"); The result being the user "stays" on page 1 but is served a download.

Is there a way to send files to the browser after downloading?

However, if you are truly interested in doing something after the file is completely downloaded, you're out of luck, as there's no event to communicate that to the browser. The only way to do that is an AJAX upload like gmail uses when you upload an attachment.


2 Answers

When you send back a file for the user to download, that is the HTTP request. In other words, you can either have a post-back which refreshes the browser page or you can send a file for the user to download. You cannot do both without special tricks.

This is why most sites when you download a file, it first takes you to a new page that says, "Your download is about to begin", and then subsequently "redirects" you to the file to download using meta-refresh or javascript.

For example, when you go here to download the .NET 4 runtime:

http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a391abd-25c1-4fc0-919f-b21f31ab88b7&displaylang=en&pf=true

It renders the page, then uses the following meta-refresh tag to actually give the user the file to download:

<META HTTP-EQUIV="refresh" content=".1; URL=http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe" />

You'll probably have to do something similar in your app. However, if you are truly interested in doing something after the file is completely downloaded, you're out of luck, as there's no event to communicate that to the browser. The only way to do that is an AJAX upload like gmail uses when you upload an attachment.

like image 196
Kirk Woll Avatar answered Sep 21 '22 19:09

Kirk Woll


In my case, I was using MVC and I just wanted the page to refresh a few seconds after the download button was selected in order to show the new download count. I was returning the file from the controller.

To do this I simply changed the view by adding an onclick event to the download button that called the following script (also in the view):

setTimeout(function () {
        window.location.reload(1);
    }, 5000);

It fit my purpose... hope it helps someone else.

like image 41
puddleglum Avatar answered Sep 24 '22 19:09

puddleglum