redirect current page to download page in asp.net – Education Career Blog

I have problem when try auto download in another page that already been specify.

In current page, I have button to trigger to redirect page to download page, but I wish to download done automatic, even it don’t done automatic. It can be done using link to download file. File name wish to download, transfer using querystring, cookie, or session (I using cookie is this case).

The redirect page and download from link button, work perfect just like I wish. But the problem is auto download dont’ work properly.

This what I already trying to do about problem auto download:

  1. using if (!IsPostBack) condition. The download work, but the page don’t redirect to download page and even that update progress keep running. (Actually i put update progress for purpose generate file before redirect and download).

  2. using timer. When is not postback condition, I trying enable the timer with interval 2sec. When in timer event is trigger i disable timer and trying download it. But the problem is timer dont disable, keep every 2 sec to download file.

Regard.

,

You can either use Server.Execute(“downloadpage.aspx”); to execute it using c# code.
or
use javascript timer like this:

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

and then call the function startdown() using c# or javascript according to your condition for download

or
use below code for download

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

References:

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

Leave a Comment