Force download PDF

Posted on 2004-04-14
Last Modified: 2008-02-01
I have a script that I am using to force people to download PDF files. It seemed to be working yesterday, but now if I choose to open the file, it prompts me for the application to use, and if I choose to save the PDF, it tries to save it with the name of the ASP page that the code is on instead of the actual filename. My server was running MDAC v2.5, so I upgraded to v2.8 to see if this would help, but it didn't. Please help me!!

Here is the code I am using:

Response.Buffer = True

Dim strFilePath, strFileSize, strFileName

Const adTypeBinary = 1

strFilePath = server.mappath(request.querystring("docURL"))
strFileSize = request.querystring("docSize")
strFileName = request.querystring("docURL")

' Requires MDAC 2.5 to be stable
' I recommend MDAC 2.6 or 2.7
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4))

' Feel Free to Add Your Own Content-Types Here
Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".pdf"
ContentType = "application/pdf"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select

Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
Response.AddHeader "Content-Length", strFileSize
' In a Perfect World, Your Client would also have UTF-8 as the default
' In Their Browser
Response.Charset = "UTF-8"
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read

Set objStream = Nothing
Question by:nistmaru
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 10824835
Try checking the value of strFileType using response.write?  See if it's ".pdf"



Author Comment

ID: 10824872
Yup, it is ".pdf"

Author Comment

ID: 10825216
I solved it myself, the problem was that some of the files are located in a subfolder below the foldler where the script is, and I was passing that folder name and the file name in the docURL field. The script didn't know how to process a file with a "/" in the name, so once I added an extra field that only pases the name ot the file everything worked just fine.

Expert Comment

ID: 10825696

Try this.......

In 'startdownload.asp' find this......

      Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileName
      Response.AddHeader "Content-Length", strFileSize
       In a Perfect World, Your Client would also have UTF-8 as the default
       In Their Browser
      Response.Charset = "UTF-8"
      Response.ContentType = ContentType

Replace with this............ or comment the above out

      response.addheader "Cache-control", "max-age=31536000"
      response.addheader "Expires", "Mon, 26 Jul 1997 05:00:00 GMT"
      response.addheader "Content-Disposition", "attachment; filename=" & chr ( 34 ) & strFileName & chr ( 34 )
      response.addheader "Content-Length", strFileSize
      response.addheader "Content-Type", ContentType & "; name=" & chr ( 34 ) & strFileName & chr ( 34 )
      response.addheader "Content-Transfer-Encoding",  "binary"


Accepted Solution

Netminder earned 0 total points
ID: 10830131
User resolved; closed, 350 points refunded.

Site Admin

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question