Click to See Complete Forum and Search --> : Initiate a Download.


TheCPUWizard
January 1st, 2003, 10:42 PM
I have a Web application with all the back end in C#.

Based on some calculations I need to download a file to the users computer. The standard "You are Downloading.. Open,Save,etc" box is fine.

I just can not remember (it is after midnight on a long day after a longer night) how to initiate the download from server code..... :o


Thanx!

Arild Fines
January 2nd, 2003, 03:53 PM
Uhm, just provide a link to the file?

TheCPUWizard
January 2nd, 2003, 05:31 PM
File is dynamically based on code, not a hyperlink.

Consider a simplified scenario. There is a textbox and a button. If the user types "Hello" in the text box and presses the button I want to build a (Text) file of every person I know and download id. If he types "Buy" then I want to dynamically creqte a database file and download it as an MDB file.
If he types anything else, no download occurs, instead the contents of the page are changed via the PostBack.

Arild Fines
January 3rd, 2003, 01:28 PM
Have a page that doesn't output any HTML, but merely writes the file back to the Response stream.

Something like this:

//write to the client
Response.ClearContent();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader( "content-disposition", "attachment; filename=" +
filename );
Response.Write( source.Source );

Response.End();


You could provide the filename or other identifier as a request parameter, eg <a href="download.aspx?id=moo.mdb>Download</a>

TheCPUWizard
January 3rd, 2003, 04:41 PM
That solution works fine, provided that the file type is not one recognized by the broswer. If (for example) you try to download an HTML page this way it will open up in the browser rather than being downloaded to the user's computer as a file. :(

Arild Fines
January 4th, 2003, 01:39 PM
I dont think the browser is supposed to open anything with a MIME type of application/octet-stream. Some browsers still do, unfortunately.

TheCPUWizard
January 4th, 2003, 01:56 PM
No my point is the opposite. I do NOT want the browser to open the file EVER (even if it can), I want to make sure that the file gets downloaded as a file (after the standard message boxes of course).

Having my client do a File->Save As is not a viable option.

Arild Fines
January 5th, 2003, 10:44 AM
Originally posted by TheCPUWizard
No my point is the opposite. I do NOT want the browser to open the file EVER (even if it can), I want to make sure that the file gets downloaded as a file (after the standard message boxes of course).

How is that the opposite?

TheCPUWizard
January 5th, 2003, 11:02 AM
I will re-run the test code, to see if I missed something. the sample did not seem to work, but it was late when I tested it....

KSCemployee
February 24th, 2004, 01:56 PM
Originally posted by Arild Fines
Have a page that doesn't output any HTML, but merely writes the file back to the Response stream.

Something like this:

//write to the client
Response.ClearContent();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AddHeader( "content-disposition", "attachment; filename=" +
filename );
Response.Write( source.Source );

Response.End();


You could provide the filename or other identifier as a request parameter, eg <a href="download.aspx?id=moo.mdb>Download</a>

This is very helpful, but I am having one problem. on the Response.Write I have the full path and file name of the local file to the server, how would I format the source.Source part? Say for instance the full paht of the file on the server was d:\storage\document5.tif??