SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to force pdf download - not open in browser window

    Hi,

    How do I force a pdf to be downloaded rather than opened in a browser window?

    I've got the code below, which seems to work in Firefox, but in IE7 it spits out a huge page of wierd characters:

    Code:
    <% 
        Response.ContentType = "application/pdf"
        FPath = server.mappath("pdf/portfolio.pdf") 
        Response.AddHeader "Content-Disposition","attachment; filename=" & FPath
        Set adoStream = CreateObject("ADODB.Stream") 
        adoStream.Open() 
        adoStream.Type = 1 
        adoStream.LoadFromFile(FPath) 
        Response.BinaryWrite adoStream.Read() 
        adoStream.Close 
        Set adoStream = Nothing 
        Response.End 
    %>
    Thanks for any pointers...

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try setting the mime type to
    application/octet-stream

    That has worked for me.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bingo, thanks!

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi everyone! thanx also, this is gr8!

    I am having the problem of when saving the pdf, it saves it under the filename of the name of the page. So if the processing page name is "dl.asp", it saves the pdf as "dl.pdf".

    I have a 1st page (default.asp) with a select-menu for users to choose a pdf, and once submit is clicked it sends to "download.asp" so that the respective pdf is able to be downloaded or opened.

    Any advice or is there a better way to this?

  5. #5
    SitePoint Enthusiast jeffrey.pry's Avatar
    Join Date
    Oct 2006
    Location
    United States of America
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not that I know of, I think you have the best method.
    -----------------
    Jeffrey Kevin Pry

  6. #6
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To change the filename of the downloaded file, just amend the code where it says

    Code:
    filename=" & FPath
    to

    Code:
    filename=myfilename.pdf

    You can then obviously pass in a variable from the submitting form to name the pdf with a name appropriate for the selected drop-down.

  7. #7
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, here is a simplest way to force the pdf to download..

    http://mushutricks.blogspot.com/2010/11/forcing-pdf-to-download-in-httaccess.html
    Last edited by Mittineague; Nov 24, 2010 at 15:12. Reason: removing unnecessary link

  8. #8
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by janzell View Post
    hi, here is a simplest way to force the pdf to download..

    http://mushutricks.blogspot.com/2010/11/forcing-pdf-to-download-in-httaccess.html
    I don't believe .htaccess files are recognized on a windows server which is required for Classic ASP
    Last edited by Mittineague; Nov 24, 2010 at 15:13. Reason: delinkifying quote


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •