By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,703 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,703 IT Pros & Developers. It's quick & easy.

question regarding redirecting through code...

P: n/a
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.

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Use Response.Redirect(fileName); on Button Click event.
Nov 18 '05 #2

P: n/a
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="Pa rtial 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

"Paul M" <mi******@hotmail.com> wrote in message news:<eC**************@tk2msftngp13.phx.gbl>...
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.

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.