Writing a binary file...

  • Thread starter Thread starter Greg
  • Start date Start date
G

Greg

Using the HttpWebRequest I'm downloading a PDF file and am trying to save it
to the hard drive, but it keeps ending up corrupt (according to adobe
acrobat). I know the PDF file isn't corrupt because I can view it if I save
it from IE.

I've tried saving it using the BinaryWriter, TextWriter, and StreamWriter.
All have produced the corrupt file.

Anyone know what the problem is?
 
set ContentType property of your HttpResponse to "application/pdf " (not
100% sure, but something like that)
then just write your binary pdf data with the BinaryWrite method


Dim bytes() As Byte = pdfFileAsByteArray
Response.Buffer = True
Response.ContentType = "application/pdf "
Response.BinaryWrite(bytes)
Response.End()


Dominique
 
Hi Greg,

I always skip things as "Writing binary file" (Armin has on that better
answers than me) but now I see you are talking about downloading.

Did you look already to the webclient.downloadfile

That is so easy to use.

I hope this helps?

Cor
 
Greg,
In addition to the other comments, it sounds like you are not reading a
binary file!

Either on your read or your write (or both) you have an System.Text.Encoding
object set, as BinaryWriter, TextWriter, and StreamWriter all have an
Encoding property.

I would simply read the underlying Stream object form the HttpWebRequest for
input & FileStream for output and read & write blocks of raw bytes. Thus
avoiding any potential encoding problems.

In other words I would not use a BinaryWriter, TextWriter, and StreamWriter
nor a BinaryReader, TextReader or a TextWriter.

Hope this helps
Jay
 
Greg said:
Using the HttpWebRequest I'm downloading a PDF file and am trying to save it
to the hard drive, but it keeps ending up corrupt (according to adobe
acrobat). I know the PDF file isn't corrupt because I can view it if I save
it from IE.

I've tried saving it using the BinaryWriter, TextWriter, and StreamWriter.
All have produced the corrupt file.

Anyone know what the problem is?

Thanks for everyones help. I was finally able to write in binary format.
 
Back
Top