Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VB.NET Download PDF Files. Seems to work, but files are incomplete/corrupt.

124 views
Skip to first unread message

Robert Keller

unread,
Jul 8, 2004, 1:33:46 PM7/8/04
to
Am using this code, which works fine for HTML (ASCII), and partially
works for PDF (binary) files:

strURL = "http://www.websitename.com/xxx.pdf"
[begin snip]
Dim objStream As System.IO.Stream
Dim objReader As System.IO.StreamReader
Try
Dim objRequest As System.Net.HttpWebRequest =
System.Net.WebRequest.Create(strURL)
Dim objResponse As System.Net.WebResponse = objRequest.GetResponse
objStream = objResponse.GetResponseStream
objReader = New System.IO.StreamReader(objStream)
Return objReader.ReadToEnd
Catch ex As Exception
Return ""
Finally
objStream.Close()
objReader.Close()
End Try
[End snip]

What happens:
1. Using IE6, the file downloads fine, and opens as expected with
Adobe on my workstation (it's a good file on from the Website.)
1. The above VB.NET code executes. No execution errors.
2. The PDF file downloads, but is incomplete:
o The first part of the PDF file (ASCII text) matches the
original
o The last part of the PDF file (also ASCII text) matches the
original
3. The file size is about 60% of what it should be.
4. Adobe opens the file, if downloaded by IE
5. Adobe dies on an error (14) if downloaded with the code above.

We suspect this is related to a binary download (ASCII chars > 128).
Upper ASCII charters ARE downloaded, just not all of them?

Should I be using another VB.NET object/method? Microsoft's Website
shows ASP code, using the RESPONSE object.

Robert Keller

no potted meat@hotmail.com David Browne

unread,
Jul 8, 2004, 2:53:08 PM7/8/04
to

"Robert Keller" <rkel...@cox.net> wrote in message
news:64139456.04070...@posting.google.com...

> Am using this code, which works fine for HTML (ASCII), and partially
> works for PDF (binary) files:
>
> strURL = "http://www.websitename.com/xxx.pdf"
> [begin snip]
> Dim objStream As System.IO.Stream
> Dim objReader As System.IO.StreamReader
> Try
> Dim objRequest As System.Net.HttpWebRequest =
> System.Net.WebRequest.Create(strURL)
> Dim objResponse As System.Net.WebResponse = objRequest.GetResponse
> objStream = objResponse.GetResponseStream
> objReader = New System.IO.StreamReader(objStream)

The StreamReader converts the bytes to chars using an decoder. You don't
want to do that. Use ObjStream.Read to read the bytes directly.

You might just use the System.Net.WebClient.DownloadFile method. It already
does this.

David


Robert Keller

unread,
Jul 8, 2004, 11:28:30 PM7/8/04
to
> The StreamReader converts the bytes to chars using an decoder. You don't
> want to do that. Use ObjStream.Read to read the bytes directly.
>
> You might just use the System.Net.WebClient.DownloadFile method. It already
> does this.
>
> David

David,

"System.Net.WebClient.DownloadFile" worked the first time. Thanks a million

RK

0 new messages