CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Question 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

  2. #2
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    Uhm, just provide a link to the file?

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  4. #4
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    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>

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  6. #6
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    I dont think the browser is supposed to open anything with a MIME type of application/octet-stream. Some browsers still do, unfortunately.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  8. #8
    Join Date
    Oct 2001
    Location
    Norway
    Posts
    265
    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?

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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

  10. #10
    Join Date
    Oct 2003
    Posts
    16
    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
  •  





Click Here to Expand Forum to Full Width

Featured