?
Solved

File download with ASp

Posted on 2011-03-03
6
Medium Priority
?
886 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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…
In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
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 sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

752 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