Library code snippets

Force 'file download' for known file types

Multipurpose Internet Mail Extensions (MIME) is a wonderful thing,
allowing your Web browser to automatically display content of many
different formats natively within the browser window. However,
sometimes, you may want users to download this content to their
client computer rather than simply viewing it in the browser. But
how do you override the browser's determination to render known
MIME types itself? The answer is to use a content disposition
header in your Web page.

Suppose you've written an ASP page that contains a link to a known
MIME type, but you want the user to download the file instead of
viewing it. Add the following to your script:

response.addHeader "content-disposition", "attachment;
filename=filename.ext"

Then substitute the actual filename and extension, and it's as good
as done. When your users click on the link, they'll immediately see
the download dialog box instead of the file's contents.

Comments

  1. 31 May 2005 at 19:58
    Does this also work to write a file from memory data to a client machine?
  2. 17 May 2005 at 08:30

    Will the same code work in netscape/mozilla

  3. 01 Dec 2004 at 08:45

    Hi,


    I am simply trying to create the File Download Box when a user clicks on file  links. Can you help? Is the code you listed all that is neeeded? I have tried to search for a reliable way on the net and found nothing. Is your code working properly?


    Thank you in advance and hope you can help!


    Nima

  4. 02 Aug 2004 at 23:32

    I want to open an xml file in asp.net.
    I have used the following lines in my code :
    Response.ContentType = "application/vnd.ms-xml";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName );


    My problem is that the xml file is opening in the parent window.
    I want to open the xml file in a new browser.
    Kindly help me with any solution.

  5. 14 Jun 2004 at 09:39
    I am trying to force a download to a HTA page in a HTML page, but how do I do this, that script given in a later posting doesn't work, which I figured since I am not creating an .asp page?
  6. 18 Feb 2004 at 09:38

    Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.


    Thanks

  7. 18 Feb 2004 at 09:38
    Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.

    Thanks
  8. 18 Feb 2004 at 09:37

    Did you ever figure out how to get the response? I need to process differently if user hits the cancel button.


    Thanks

  9. 12 Nov 2003 at 07:56

    Add :
    Call Response.Flush()
    Call Response.End()


    This code works for ASP 3.0, so for IIS 5.0, not only for .aspx pages.

  10. 07 Nov 2003 at 00:07

    I was able to download the file using the above code, but in the downloaded the page.aspx html content is also getting added in the downloaded file.


    I'm using the below code in Page.aspx


    Response.AddHeader("Content-Disposition", "attachment;filename=" & "TextFile1.txt")
    Response.ContentType = "text/plain"
    Response.WriteFile("TextFile1.txt")


    So, the donloaded file will have TextFile.txt contents + the HTML content of Page.aspx

  11. 21 Oct 2003 at 02:41

    Yes. I also have the same problem.


    How do you force download on MAC IE? Can anyone help?

  12. 03 Sep 2003 at 11:15

    Hi,  I have no problem forcing the download box to show up with ASP, but I want to do more with it.  The download box has 4 buttons, OPEN, SAVE, CANCEL and HELP.  I'd like to know what the user selected and do something different.  Specifically, if the user click OPEN OR SAVE, I want to increment a counter of how many times the file has been downloaded.  If the user click CANCEL, I want to do nothing.


    My question is:  How can I do that?  Thanks.


    P.S.  Here is the code that I use to force download box:
       Response.Buffer = True
       Response.Clear
       Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
       Response.AddHeader "Content-Length", objFile.Size
       Response.ContentType = "application/octet-stream"
       Response.CharSet = "UTF-8"
       
       '...
       ' code to write out binary stream of the file to browser buffer
       '...
       
       Response.Flush


  13. 08 Aug 2003 at 09:38

    I used the given code and used it to invoke a file download box.. It worked also and when I gave a location   and saved the file.. the file got saved (with the same name) but it was empty... that is no content... please help out

  14. 03 Feb 2003 at 07:59
    This worked for me:

    Code:

    response.addheader "content-disposition","attachment; filename='" & Filename & "'"
    server.transfer Filename
  15. 07 Jan 2003 at 00:39

    hi, all


    i understand the use of ADDHeader method, but there's a slight problem with this.....


    got a page (page1.asp) from which the user selects certain values and on click of a button (page2.asp) is displayed andthe results are shown in a HTML table.... so far so good.
    Page2.asp has a link for downloading the filein Pipe format or Excel..... now what needs to be done is once the link is clicked the file needs to generated and prompted for "Save" .....
    i tried adding the Header but then it asks to save the (page2.asp) itself......


    can this be solved


    Cheers !!!
    Niraj

  16. 18 Oct 2002 at 02:39

    [1]How do I force a file download from a MAC without the file opening in the browser?[/1]

  17. 16 Oct 2002 at 06:49
    I am experiencing a similar problem with downloads on mac.
    Can you point me to the articles you found.
    Regards
  18. 21 Aug 2002 at 06:17
    Here's an example:
    [courier new]
    <% @LANGUAGE = VBScript %>
    <%
    Option Explicit
    Response.AddHeader "content-disposition","attachment; filename='test.xls'"
    %>

    <html>
    <head>
    <title>Save file</title>
    </head>
    <body>
    <HREF="test.xls">TEST.XLS</A>
    </body>
    </html>
    [/courier new]
  19. 16 Aug 2002 at 11:42

    I've read articles which stated that the Mac IE 5.1 is at fault here.


    Need to get Microsoft to fix this!!!!!

  20. 15 Aug 2002 at 11:31

    I am having same problem on Mac.  Could not find any help on this so far.  Could you please reply if you found a solution.

  21. 13 Jun 2002 at 15:16

    Use the filesystem object.  It is MUCH slower than ADO stream, but it will get the job done.


    Quote:
    [1]Posted by tsorial on 24 May 2002 03:55 PM[/1]
    How can the download be forced if the link to the file is known: that is the file is being downloaded to client's machine from a remote server besides mine.  


    I have tried using "ADODB.stream" object to do this.  But, my server has ADO 2.1 and ADO 2.5 or higher is needed for the stream object to work. I am not allowed to upgrade the ADO on this server.  Your urgent help is greatly appreciated.

  22. 27 May 2002 at 17:13

    May someone please help me with force download problem.  I need this urgently.  I greatly appreciate your help.

  23. 24 May 2002 at 15:55

    How can the download be forced if the link to the file is known: that is the file is being downloaded to client's machine from a remote server besides mine.  


    I have tried using "ADODB.stream" object to do this.  But, my server has ADO 2.1 and ADO 2.5 or higher is needed for the stream object to work. I am not allowed to upgrade the ADO on this server.  Your urgent help is greatly appreciated.

  24. 05 May 2002 at 01:34

    Hi,


    I tried your tutorial.  Works great on Windows and IE 5.5+.  But my Mac running IE 5.1 doesn't work.  The Save As dialog did not show up on Mac.


    Please help.

  25. 01 Jan 1999 at 00:00

    This thread is for discussions of Force 'file download' for known file types.

Leave a comment

Sign in or Join us (it's free).

ElementK Journals
AddThis

Related discussion

Related podcasts

  • Scott Guthrie

    Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.

Events coming up

Want to stay in touch with what's going on? Follow us on twitter!