2013-04-10 142 views
0

後ASP.NET開始下載我有這樣的代碼來啓動一個下載的頁面被顯示給用戶後:頁面加載

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

但重定向的頁面加載和頁面加載從來沒有做過。

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

我無法使用Transmit.File,因爲該文件位於不同的服務器上。

回答

2
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> 
+0

我試過這個,但是頁面沒有顯示,我已經添加了15秒的延遲,並且在延遲之後下載開始時不顯示頁面。 – 2013-04-10 12:33:52

+0

嘗試上面的代碼,... 9000是發生重定向後的延遲毫秒數 – 2013-04-10 12:48:49

1

我使用下面的代碼,彈出一個對話框,以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(); 
+0

我無法使用寫入或傳輸文件,因爲該文件位於另一臺服務器上。 – 2013-04-10 15:44:46