Solved

File download with ASp

Posted on 2011-03-03
6
854 Views
Last Modified: 2012-05-11
I am using the following code to allow signed-in users to download a pdf. The download works but then adobe reports that the file is damaged. NOTE: the file on the server is NOT damaged - the corruption seems to occur during download.

leadno = request("leadno")
filename =  "OBS"   & leadno & ".pdf"

strFilePath = "D:\inetpub\vhosts\xxxxxxxxxx\httpsdocs\observations\"   & filename
response.write(strFilePath)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.GetFile(strFilePath)
intFileSize = objFile.Size
Set objFile = Nothing

strFileName = filename
strFileName = replace(filename," ","-")
Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName

Response.ContentType = "application/x-msdownload"
Response.AddHeader "Content-Length", intFileSize

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS And Response.IsClientConnected
Response.BinaryWrite objStream.Read(1024)
Response.Flush()
Loop
objStream.Close
Set objStream = Nothing
Else
Response.write "...Error finding file."
response.write(strFilePath)
Response.write "..Error finding file."
End if
Set objFSO = Nothing
0
Comment
Question by:cdjohn31
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:R-Byter
ID: 35026250
Put this as content type:

Response.ContentType = "application/pdf"

Open in new window


Regards
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 35026259
To force download, put this:

Response.ContentType = "application/octet-stream"

Open in new window


Regards
0
 

Author Comment

by:cdjohn31
ID: 35027708
I have tried both of these - same result
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Expert Comment

by:R-Byter
ID: 35027830
Are those download files bigger or smaller then original ones? What is their size after download?

Regards
0
 

Author Comment

by:cdjohn31
ID: 35047108
The size on the server and after download are identical
0
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 35055943
Your code supposes that the file has a size that is an exact multiple of 1024 bytes. That is probably not the case.

I use a function as below.
Sub WriteFileToBrowser(ByVal sFilePath, ByVal sContentType, ByVal sDisplayName)
    Dim oStream, bBuffer, iFileSize, iChunk, i

    Response.Buffer = False
    Server.ScriptTimeout = 30000
    iChunk = 64000

    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    iFileSize = oStream.Size

    Response.ContentType = sContentType
    Response.AddHeader "Content-Disposition", "filename=" & sDisplayName
    Response.AddHeader "Content-Length", oStream.Size

    For i = 1 To iFileSize \ iChunk
        If Not Response.IsClientConnected Then Exit For
        Response.BinaryWrite oStream.Read(iChunk)
    Next

    If iFileSize Mod iChunk > 0 Then
        If Response.IsClientConnected Then
            Response.BinaryWrite oStream.Read(iFileSize Mod iChunk)
        End If
    End If
    oStream.Close
    Set oStream = Nothing
End Sub

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

The ability to edit PDF documents can be useful, however it may not be a straight forward process. Many non-technical people don't realise that a PDF document is basically an image rather than a text file, even if it contains nothing but text. If…
This article is in response to a question here (http://www.experts-exchange.com/Other/URLs/Q_28283850.html) at Experts Exchange. The Original Poster has a scanned signature and wants to make the background transparent so that the signature may be pl…
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now