Solved

File download with ASp

Posted on 2011-03-03
6
843 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article focuses on how to remove password security from multiple PDF files by Adobe Acrobat program. Sometimes it is essential to access the stored data items and to print, edit as well as copy content from Portable Document Format files in abs…
*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …

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

21 Experts available now in Live!

Get 1:1 Help Now