File downloads to client

G

Gibby

A few weeks back, a message was posted regarding how to force a file
download to a client. The code for this technique is shown at the end
of this message.

A question I have about this technique is how to gracefully get back
to one's web app after doing the file download? Does one just do the
transfer by opening the download page in a separate browser window and
leave the "empty" browser for the user to close or is there a more
elegant way to handle this?

TIA,

Paul


======================================================

private void Page_Load(object sender, System.EventArgs e)
{
string path = Server.MapPath(Request.ApplicationPath +
"/test.acp");
System.IO.FileInfo file = new System.IO.FileInfo(path);

// clear the current output content from the buffer
Response.Clear();

// add the header that specifies the default filename for the
// Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename="
+
file.Name);

// add the header that specifies the file size, so that the
browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());

// specify that the response is a stream that cannot be read by
the
// client and must be downloaded
Response.ContentType = "application/octet-stream";

// send the file stream to the client
Response.WriteFile(file.FullName);

// stop the execution of this page
Response.End();
}
 
A

avnrao

when file download is clicked..using javascript open a new window
window.open("yourdownload.aspx")
and in yourdownload.aspx ..put the code to download and just before
Response.End() (code below)
put RegisterStartupScript("closeWindow","<script
language='javascript'>window.close();</javascript>") // closes the window

hth,
Av.
 
P

Paul Gibson

I had thought of using a technique similar to what you suggested but
without any success. I believe that it fails because that the info I
put into the header instructs the browser to not to attempt to render
the data stream and this forces the download dialog to appear. In fact,
after the download completes, if I attempt to view the source for the
page, there is none. Additionally, the address line of the browser (IE6)
is blank.


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
B

Brian Lowe

I do exactly this in a couple of my web applications, but don't have a
problem.

I set the target attribute on the page that calls for the download to _self,
so the browser uses the same window. When the browser gets the response
forcing a download it downloads the file, leaving the original page in
view - no new window to close, no empty window, just what was there before.

Brian Lowe
---------@
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top