Solved

File download with ASp

Posted on 2011-03-03
6
865 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
Power PDF (http://www.nuance.com/for-business/document-imaging-and-scanning/power-pdf-converter/index.htm) is the newest product from the Document Imaging division of Nuance Communications (http://www.nuance.com/). It is available in two editions — …
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

749 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