|
-
January 1st, 2003, 11:42 PM
#1
Initiate a Download.
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..... 
Thanx!
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 2nd, 2003, 04:53 PM
#2
Uhm, just provide a link to the file?
-
January 2nd, 2003, 06:31 PM
#3
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 3rd, 2003, 02:28 PM
#4
Have a page that doesn't output any HTML, but merely writes the file back to the Response stream.
Something like this:
Code:
//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>
-
January 3rd, 2003, 05:41 PM
#5
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 4th, 2003, 02:39 PM
#6
I dont think the browser is supposed to open anything with a MIME type of application/octet-stream. Some browsers still do, unfortunately.
-
January 4th, 2003, 02:56 PM
#7
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.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 5th, 2003, 11:44 AM
#8
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?
-
January 5th, 2003, 12:02 PM
#9
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....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 24th, 2004, 02:56 PM
#10
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:
Code:
//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??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|