当尝试在另一个已经指定的页面自动下载时,我遇到了问题。
在当前页面中,我有按钮要触发重定向页面到下载页面,但我希望下载完成自动,即使它不自动完成。可以使用链接下载文件。文件名希望下载、使用querystring、cookie或会话进行传输(本例就是使用cookie )。
重定向页面,从链接按钮下载,工作完美,正如我所希望的。但问题是自动下载不能正常工作。
这就是我已经尝试过的关于自动下载问题的方法:
if (!IsPostBack)条件。下载工作,但页面不重定向下载页面,甚至更新进程继续运行。(实际上,在重定向和download).请注意。
发布于 2010-10-21 05:02:50
您可以使用Server.Execute("downloadpage.aspx");使用c#代码执行它。或者使用javascript计时器,如下所示:
function startdown() {
var url = "<%= DownloadPageurl %>";
setTimeout("window.location.href='" + url + "';", 5000);
}然后根据下载条件使用c#或javascript调用函数star倒计时()
或使用下面的代码下载。
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
https://stackoverflow.com/questions/3984384
复制相似问题