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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iis reverse proxy virtual directory 8 134
Choose the older file FSO 6 42
Server Timeout with Loop 6 38
File download script VBSCRIPT fail on download PPT files 2 36
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now