Solved

Force download PDF

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

19 Experts available now in Live!

Get 1:1 Help Now