Solved

Force download PDF

Posted on 2004-04-14
6
2,584 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
6 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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