title: ASP.NET download file using browser and redirect to another page at the same time
body: <p>Some time ago I had the following code in authorization routine:</p>
  
  <pre><code> protected void Login1_LoggedIn(object sender, EventArgs e)
          {
              string returnUrl = Request.QueryString["ReturnUrl"];   
              if (!string.IsNullOrEmpty(returnUrl))
              {
                  // download file from server
  
                  Response.Redirect(returnUrl);
              }
          }
  </code></pre>
  
  <p><code>returnUrl</code> is a full path to the file on the server, so I used to download the file via browser download standard dialog this way.
  But now I need to redirect user to another page right after authorization, at the same time when downloading file. So now I need something like</p>
  
  <pre><code>{
      // download file from server
      Response.Redirect(returnUrl);
      Response.Redirect("/");
  }
  </code></pre>
  
  <p>I cannot find the way I can do that correctly. I tried a lot of approaches, but nothing helped. The important note is I cannot use the intermediate page like download.aspx that is used only for downloading and then redirects to the necessary page.
  So, the approaches I used:</p>
  
  <p>1.</p>
  
  <pre><code>Response.Redirect(returnUrl, false);
  Response.Redirect("/");
  </code></pre>
  
  <p>Result: the file does not downloading, immediate redirect happens.</p>
  
  <p>2.</p>
  
  <pre><code>Response.Redirect(returnUrl, false);
  Response.Redirect("/", false);
  </code></pre>
  
  <p>Result: nothing happens.</p>
  
  <p>3.</p>
  
  <pre><code>Response.Clear();
  Response.ClearHeaders();
  Response.ClearContent();
  Response.ContentType = "application/octet-stream";
  Response.AddHeader("Content-Disposition", "attachment; filename=" + returnUrl);
  Response.TransmitFile(Server.MapPath("~" + returnUrl));
  Response.End();
  Response.Redirect("/");
  </code></pre>
  
  <p>Result: File is downloaded, but no redirect. When remove <code>Response.End();</code> redirect happens, but file is not loading.</p>
  
  <ol>
  <li>The previous variant with following changes:
  Added:
  Response.AddHeader("Refresh", "3; URL=/home.aspx");</li>
  </ol>
  
  <p>Removed:
  Response.Redirect("/");</p>
  
  <p>Result: File is downloading, but no refresh/redirect happens.</p>
  
  <ol>
  <li>Used javascript for downloading file:</li>
  </ol>
  
  <p>Response.Write("language='javascript'>window.location.assign('returnUrl','download', '');");
  Response.Redirect("/");</p>
  
  <p>Result: File is not loading, redirect happens.
  Replaced <code>Response.Write</code> parameters with <code>"&lt;script&gt;alert('test');&lt;/script&gt;"</code>. No alert appears, redirect happens.</p>
  
  <p>Do you know, what's the problem?</p>