Solved

File download with ASp

Posted on 2011-03-03
6
862 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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

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…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
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…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…

839 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