Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting a page after a PDF download

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?

like image 574
priyanka.bangalore Avatar asked Dec 09 '22 17:12

priyanka.bangalore


2 Answers

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 :)

like image 84
wintermute Avatar answered Dec 23 '22 10:12

wintermute


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.

like image 45
WVDominick Avatar answered Dec 23 '22 12:12

WVDominick