Why does this download pdf file code from a non-direct link damage the file?

Posted on 2009-02-09
Last Modified: 2012-08-14
The code allows download.
But when the file is opened.
The file is damaged.

<%Option Explicit%>


	Dim adoStream, stream, fileName, mimeType

	Set adoStream = Server.CreateObject("ADODB.Stream")

	fileName = Request.QueryString("file")

	mimeType = Request.QueryString("mime")


	adoStream.Type = adTypeBinary

	'adoStream.LoadFromFile Server.MapPath(fileName)

	adoStream.LoadFromFile Server.MapPath("/s1QZAkAeAKT1chsRu/" & fileName)





	If adoStream.Type = adTypeBinary Then

	stream = adoStream.Read()


	stream = adoStream.ReadText()

	End If


	Set adoStream = Nothing

	Response.Buffer = True

	Response.AddHeader "content-disposition", "attachment; filename=" & fileName

	Response.AddHeader "content-length", Len(stream)

	Response.ContentType = mimeType

	Response.BinaryWrite stream



Open in new window

Question by:RepressedRadar
    1 Comment
    LVL 12

    Accepted Solution

    Don't ask why, but try adding the below to the very top of your page and see what happens?   If it works you will need to redo your code as this is a special call for PDFs files and will most likely break all other file types - the whole thing gets a bit technical and I can't remember where I read the article that suggested this.
    <!--METADATA TYPE="TypeLib" UUID="{414FEE4B-2879-4090-957E-423567FFCFC6}"-->

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    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…
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 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

    21 Experts available now in Live!

    Get 1:1 Help Now