?
Solved

Force download PDF

Posted on 2004-04-14
6
Medium Priority
?
2,594 Views
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")
<%
Response.Clear

'8*******************************8
' Requires MDAC 2.5 to be stable
' I recommend MDAC 2.6 or 2.7
'8*******************************8
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
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/vnd.ms-excel"
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
Response.Flush

objStream.Close
Set objStream = Nothing
%>
0
Comment
Question by:nistmaru
5 Comments
 
LVL 10

Expert Comment

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

--------
D_M_D

0
 

Author Comment

by:nistmaru
ID: 10824872
Yup, it is ".pdf"
0
 

Author Comment

by:nistmaru
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.
0
 
LVL 2

Expert Comment

by:Fataqui
ID: 10825696
Hi


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"



Fataqui!
0
 
LVL 5

Accepted Solution

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

Netminder
Site Admin
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

621 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