views:

866

answers:

4

I am using ASP.NET2.0. I have created a download form with some input fields and a download button. When the download button is clicked, I want to redirect the user to a "Thank you for downloading..." page and immediately offer him/her the file to save.

I have this following code to show the savefile dialog:

public partial class ThankYouPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     Response.Clear();
     Response.AddHeader("Content-Disposition", 
                          "attachment; filename=\"downloadedFile.zip\"");
     Response.ContentType = "application/x-zip-compressed";
     Response.BinaryWrite(this.downloadedFileByteArray);
     Response.Flush();
     Response.End();
    }
}

Obviously, this code does not allow to display any "Thank you" message. Is there an "AfterRender" event or something similar of the Page where, I could move this download code and give a chance for the page to render the "thank you" message to the user? After all, I am truely thankful to them, so I do want to express that.

+5  A: 

You could reference a download page from your thank you page using an IFrame

<iframe src="DownloadFile.aspx" style="display:none;" />

In this case, DownloadFile.aspx would have the code behind from your example.

Phil Jenkins
+1  A: 

Use a META REFRESH tag in the head of your thank-you page:

<META http-equiv="refresh" content="1;URL=http://site.com/path/to/downloadedFile.zip"&gt;

Alternatively, you might use a body onLoad function to replace the current location with the download URL.

<body onLoad="document.location='http://site.com/path/to/downloadedFile.zip'"&gt;

In this case the redirection will start after the current page has finished loading and only if the client has JavaScript enabled, so remember to include a link with a download link ("If your download doesn't start in a few seconds..." and so on).

You may also use an IFRAME as suggested by Phil, or even a FRAME or a full-blown pop-up (blockable, mind you). Your mileage may vary.

codehead
+1  A: 

The code you've written, should actually be redirected to from the 'thank you' page (making it the 2nd redirect). Because you've set the content disposition to attachment, this page will not actually replace the existing 'thank you' page.

sandesh247
+1  A: 

if you want to serve a "Thank You" page and the file the client must call twice the server. So you can just create the thankyou.aspx page for displaying the message (and maybe put a direct download link to the file). You can start download with a meta tag or just using js (even ms do the same for their download page).

Then to serve the file you sould create a direct link to avoid another page run on the server; otherwise you should create an HttpHandler just to hide the filesys.

The file should be sent to the client with Response.TrasmitFile

Andrea Balducci