0

I have a button. When the user clicks it, I want to trigger a download dialog box, and then redirect the user to another page.

However, I'm having problems getting the download and redirect to run together. Once the download dialog box is shown, execution on the rest of the code seems to end:

Sub DisplayDownloadDialog(ByVal filepath As String)
    Dim filename As String = Path.GetFileName(filepath)
    Response.ContentType = "application/octet-stream"
    Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")
    Response.WriteFile(filepath)
End Sub

I tried to add a refresh to the header, but it still doesn't work. I also tried javascript (but not using window.open...since some browsers block that), but I can't get a redirect to work.

Any ideas? I really appreciate the help.

Erin
  • 1
  • 1

2 Answers2

0

Try the following

Suppose you have three pages like

  1. Content.aspx --> This page will have a download link
  2. Thanks.aspx --> The page to which you want to redirect ie this page will show a thanks note
  3. Download.aspx --> This page will do the actual download function.

Now in Content.aspx

You have a link button/image button/button control called btnDownload In Page load of Content.aspx you can add the following code in the page load

protected void Page_Load(object sender, EventArgs e)
    {

        btnDownload.Attributes.Add("onclick", "Javascript:GotoDownloadPage();return    
        false;");

    }

Add the following script function in the source of Content.aspx

    <script type="text/javascript" language="javascript">

   function GotoDownloadPage()
    {
        window.location = "Thanks.aspx";
        return false;
    }
    </script>

Now in Thanks.aspx

Add the following code in the page load

protected void Page_Load(object sender, EventArgs e)
    {

            if (!IsPostBack)
            {
                ScriptManager.RegisterStartupScript(Page, GetType(), "Redirect", 
                "<script>GotoDownloadPage()</script>", false);
            }

    }

Add the following script function in the source of Thanks.aspx

 <script type="text/javascript" language="javascript">

   function GotoDownloadPage()
    {
        window.location = "Download.aspx";
        return false;
    }
    </script>

Now In Download.aspx

Add the logic for downloading file

Hope this will help you..

Sushanth.B
  • 11
  • 2
0

Once you call Response.WriteFile(filepath), the response is over and Redirect is not going to work

You may try this:

On button's OnClientClick event, using windows.open() to open a new .aspx page, named like "download.aspx". and in button's postback event, do your redirect.

In the "download.aspx" page, your write your download code.

I think it should work,

fengd
  • 7,551
  • 3
  • 41
  • 44