question regarding redirecting through code...

P

Paul M

hi there,

i have an <asp:button> control on my webpage that when clicked, i want to
redirect the HTTP request to an EXE file, but instead of actually directing
the browser and address bar to there, i only want the SaveAs box to appear,
to download the file, and the actual web page to remain the same.

VB.NET example would be greatly appreciated.

Thanks,
Paul.
 
I

Ian

I dont do VB.NET, but I made a page that does effectively what you
ask:

You'll need to set the content-type as appropriate (or
application/octet-stream or whatever) and most of the junk in the
middle is to support resuming downloads, but basically make an empty
ASPX page with this Page_Load and then pass the relative path to the
file you want to force download on the query string (i.e.
forceDownload.aspx?File=~/foobar/baz.pdf -- URLEncoded of course)

private void Page_Load(object sender, System.EventArgs e)
{
string webAppPath = Request.Params["File"];
string fileMapPath;
try
{
fileMapPath = Server.MapPath(webAppPath);
}
catch { return; }

if (!File.Exists(fileMapPath))
return;

FileInfo infoAboutFile = new FileInfo(fileMapPath);
long startPos = 0, fileSize, endPos;
fileSize = infoAboutFile.Length;
endPos = fileSize;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();

string range = HttpContext.Current.Request.Headers["Range"];
if (range != null && range.Length > 0)
{
string[] startEnd =
range.Substring(range.LastIndexOf("=")+1).Split('-');
if (startEnd[0].Length > 0)
startPos = Convert.ToInt32(startEnd[0]);

if (startEnd.GetUpperBound(0) >= 1 && startEnd[1].Length > 0)
endPos = Convert.ToInt32(startEnd[1]);
else
endPos = fileSize - startPos;

if (endPos > fileSize)
endPos = fileSize - startPos;

HttpContext.Current.Response.StatusCode=206;
HttpContext.Current.Response.StatusDescription="Partial Content";
HttpContext.Current.Response.AppendHeader("Content-Range", "bytes "
+ startPos.ToString() + "-" + endPos.ToString() + "/" +
fileSize.ToString());

}

if (startPos == 0)
HttpContext.Current.Response.ContentType = "application/pdf";

HttpContext.Current.Response.AppendHeader("Content-disposition","attachment;
filename=" + infoAboutFile.Name);
HttpContext.Current.Response.WriteFile(fileMapPath, startPos,endPos);
HttpContext.Current.Response.End();
}

The key to doing what you want to do here is the
content-disposition:attachment header which will force modern browsers
to download instead of attempting to display or hand off to other app.

Hope that helps
Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top