简体   繁体   中英

ASP.NET Start Download after Page Load

I have this code to start a download after the page was displayed to the user:

 protected void Page_Load(object sender, EventArgs e)
    {
        Response.Redirect("http://myserver.com/myfile.zip", false);   
    }        

But the redirect is done before the page loads and the page never loads.

How can I start the download and finish displaying the page to the client?

I cannot use Transmit.File, because the file is located on a different server.

protected void Page_Load(object sender, EventArgs e)
{
  Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "java", "setTimeout(Redirect, 9000);", true);
}

In aspx page make a javascript function

<script language="javascript" type="text/javascript">
    function Redirect() {
        window.location = 'http://myserver.com/myfile.zip';
    }
</script>

I use the following code to popup a dialog to download a CSV file. Perhaps something like this could work for you?

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", "filename=export.csv");
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.Write(sb.ToString()); // In my case, the StringBuilder object was the file to be written.  I don't know exactly what you'd for a non dynamic file.
HttpContext.Current.Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM