4

I have an aspx (say 1.aspx) page from where first I am downloading a pdf file and then I want to redirect to some Thanks.aspx page. The code is this:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string pathId = string.Empty;
    if (Page.IsValid)
    {
        try
        {    
            pathId = hidId.Value;
            DownloadPDF(pathId);                        

            Response.Redirect("Thanks.aspx");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}



protected void DownloadPDF(string pathId)
{
    if (!(string.IsNullOrEmpty(pathId)))
    {
         try
        {
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + pathId + ".pdf");
            string path = ConfigurationManager.AppSettings["Pdf_Path"].ToString() + "\\" + pathId.Trim() + ".pdf";
            Response.TransmitFile(path);                   
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}

The problem is that, the file save dialog is coming properly and I am able to download the file also, but it is not getting redirected to the Thanks.aspx page.

How to resolve this?

GSerg
  • 76,472
  • 17
  • 159
  • 346
priyanka.bangalore
  • 1,471
  • 7
  • 20
  • 32

6 Answers6

7

If the file is just downloaded, no preprocessing is done, you could try the following:

Response.AddHeader("Refresh", "12;URL=nextpage.aspx");

Where the number is the seconds before refresh is done :)

wintermute
  • 433
  • 2
  • 12
  • this totally do the trick, without having to put javascript to handle the redirection. great ! – Thomas KiTe Trentin Jul 15 '14 at 15:39
  • This method is much better to use in any instance where you want to forward or redirect to another page after any action, after a delay. This also won't cause possible errors that Response.Redirect might by aborting a thread. If a frame isn't a good solution, this solution should definitely be used. – Joe Aug 19 '15 at 14:11
  • 1
    I concur with MichaelBrennt and Speuline. Doesn't work in Firefox either. Or maybe I just suck. – interesting-name-here May 03 '17 at 13:27
  • working in chrome Version 77.0.3865.90 (Official Build) (64-bit) – gilad Oct 06 '19 at 08:43
6

I've found it easier to put the PDF download page in an iframe. That way you can activate the PDF download on the client side by just pointing the iframe source to the PDF download page. After that you can either move to a new page, or just show the thank you text right that on the page that has the iframe in it.

WVDominick
  • 2,074
  • 15
  • 17
1

In HTTP, a request can only have a single response. Since the first response is the PDF file, the seconds response (i.e. the redirect) cannot be implemented.

You can try to redesign the two pages by redirecting to thanks.aspx and have thanks.aspx start the download automatically.

devio
  • 36,858
  • 7
  • 80
  • 143
0

A Response.Redirect actually sends a response to the browser that basically says this resource has moved to some other URL. However, you're trying to send a file down in a response too, so those two things are probably clashing with each other. Try sending back a little JavaScript that sends them to the page you want to send them too instead of using a Response.Redirect.

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "redirectScript", "window.location.href='whateverurlhere.aspx';", True)
Brandon Montgomery
  • 6,924
  • 3
  • 48
  • 71
0

See the article mentioned in this accepted answer: https://stackoverflow.com/a/11018277/1037864 (direct link: http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/)

The idea is to set a cookie and send it together with the file. Meanwhile you let the waiting page block the UI while it is waiting for the cookie to arrive.

Community
  • 1
  • 1
Björn
  • 3,098
  • 2
  • 26
  • 40
0

I tried many things (like the ideas here) but nothing worked for my particular situation. In the end for me I used an approach where my C# sets a cookie that the JavaScript looks for and the form buttons/etc are disabled accordingly until the cookie is detected.

My code is here in case anyone thinks this solution might work for you: https://gist.github.com/cemerson/9811a384d7f41bc683b2bd9ed4bf5b17

Christopher
  • 1,639
  • 19
  • 22