簡體   English   中英

頁面加載后ASP.NET開始下載

[英]ASP.NET Start Download after Page Load

頁面顯示給用戶后,我有以下代碼可以開始下載:

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

但是重定向是在頁面加載之前完成的,並且頁面從不加載。

如何開始下載並完成向客戶端顯示頁面?

我無法使用Transmit.File,因為該文件位於其他服務器上。

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

在aspx頁面中創建一個javascript函數

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

我使用以下代碼彈出一個對話框來下載CSV文件。 也許這樣的事情可能對您有用?

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM