2013-04-10 154 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