将当前页面重定向到asp.net中的下载页面

时间:2010-10-21 04:52:11

标签: asp.net redirect download

我在另一个已经指定的页面中尝试自动下载时遇到问题。

在当前页面中,我有按钮触发重定向页面下载页面,但我希望自动下载完成,即使它没有自动完成。可以使用链接下载文件来完成。文件名希望下载,使用查询字符串,cookie或会话进行传输(我使用cookie就是这种情况)。

重定向页面和从链接按钮下载,工作完美就像我希望的那样。但问题是自动下载不能正常工作。

这就是我已经尝试过的自动下载问题:

  1. 使用if (!IsPostBack)条件。下载工作,但页面不重定向到下载页面,甚至更新进度继续运行。 (实际上我在重定向和下载之前为目的生成文件放置了更新进度)。

  2. 使用计时器。当不是回发条件时,我尝试以2秒的间隔启用定时器。当在计时器事件触发时我禁用计时器并尝试下载它。但问题是计时器不能禁用,每2秒保存一次下载文件。

  3. 方面。

1 个答案:

答案 0 :(得分:1)

您可以使用Server.Execute(“downloadpage.aspx”);使用c#代码执行它。 要么 像这样使用javascript计时器:

function startdown() {
var url = "<%= DownloadPageurl %>";
setTimeout("window.location.href='" + url + "';", 5000);
}

然后根据您的下载条件使用c#或javascript调用函数startdown()

或 使用下面的代码下载

lass DownloadLibrary
{
public static string getContentType(string Fileext)
{
string contenttype = "";
switch (Fileext)
{
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".doc":
contenttype = "application/msword";
break;
case ".ppt":
contenttype = "application/vnd.ms-powerpoint";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".jpg":
case ".jpeg":
contenttype = "image/jpeg";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".ico":
contenttype = "image/vnd.microsoft.icon";
break;
case ".zip":
contenttype = "application/zip";
break;
default: contenttype = "";
break;
}
return contenttype;
}

public static void downloadFile(System.Web.UI.Page pg, string filepath)
{
pg.Response.AppendHeader("content-disposition", "attachment; filename=" + new FileInfo(filepath).Name);
pg.Response.ContentType = clsGeneral.getContentType(new FileInfo(filepath).Extension);
pg.Response.WriteFile(filepath);
pg.Response.End();
}
}

参考文献:

http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html

http://dotnetacademy.blogspot.com/2010/01/download-any-file-or-image-from.html

http://dotnetacademy.blogspot.com/2010/07/code-to-download-file-on-buttton-click.html

相关问题