471,350 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Problem Downloading PDF with HTTPWebRequesst

GJK
Hello,
I am trying to download a pdf file using HTTPWebRequest class and then
save the downloaded pdf to a SQL Server 2005 database image column and
then display the pdf on a web app.
When I try to display the PDF, Adobe reader comes up and shows an error
msg "an uncrecognized token aID was found".

Here is my code to download

Dim binByte As Byte()
ResponseStreamRdr = New StreamReader(theResponse.GetResponseStream(),
New UnicodeEncoding)
Dim objMemoryStream As New MemoryStream(New
UnicodeEncoding().GetBytes(ResponseStreamRdr.ReadT oEnd()))
binByte = objMemoryStream.ToArray()
return binByte

Then I write store the binByte to an image datatype column in SQL
Server which works.

Then I try to display the pdf using the following code where I get the
error "an uncrecognized token aID was found".

byte[] bteContent = (byte[])dr["file_content"];
Response.AddHeader("Content-Disposition","attachment;filename=" +
fileName);
Response.OutputStream.Write(bteContent, 0, bteContent.Length);
Response.Flush();
Response.End();

Any idea what is wrong with the code?

Note: One thing I noticed during debugging:
theResponse.contentlength=22069 where as objMemoryStream.Length
=21890.
Does this has to do something with pdf corruption. Why is this
difference?

Feb 14 '06 #1
4 1396
Dont set the Content-Disposition header. Instead, set the
Response.ContentType = "application/pdf"

Everything else looks ok.

Feb 14 '06 #2
i havent palyed with SQL 2005 yet, but just a shot in the dark, you
said you were storing the data in SQL as image datatype. I think you
should try binary datatype.

thats how it was in sql2k at least, not sure if the same still holds
true to sql2005

Feb 14 '06 #3
Thus wrote GJK,
Hello,
I am trying to download a pdf file using HTTPWebRequest class and then
save the downloaded pdf to a SQL Server 2005 database image column and
then display the pdf on a web app.
When I try to display the PDF, Adobe reader comes up and shows an
error
msg "an uncrecognized token aID was found".
Here is my code to download

Dim binByte As Byte()
ResponseStreamRdr = New
StreamReader(theResponse.GetResponseStream(),
New UnicodeEncoding)
Dim objMemoryStream As New MemoryStream(New
UnicodeEncoding().GetBytes(ResponseStreamRdr.ReadT oEnd()))
binByte = objMemoryStream.ToArray()
return binByte
This will likely destroy any PDF file. You're handling PDF as UTF-16 encoded
text, but it's just binary content. Discard the StreamReader and read directly
from the response stream.

Then I write store the binByte to an image datatype column in SQL
Server which works.

Then I try to display the pdf using the following code where I get the
error "an uncrecognized token aID was found".

byte[] bteContent = (byte[])dr["file_content"];
Response.AddHeader("Content-Disposition","attachment;filename=" +
fileName);
I'm nitpicking, but use AppendHeader(). AddHeader() is an ASP legacy API.
Also, set the HTTP Content-Type header for PDF:

Response.ContentType = "application/pdf";
Response.OutputStream.Write(bteContent, 0, bteContent.Length);
Response.Flush();
Response.End();
Any idea what is wrong with the code?
No, that looks fine.
Note: One thing I noticed during debugging:
theResponse.contentlength=22069 where as objMemoryStream.Length
=21890.
Does this has to do something with pdf corruption. Why is this
difference?


See above. PDF isn't UTF-16 encoded text.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Feb 14 '06 #4
GJK
Thanks for all the replies. I changed the code as follows and it is
working fine.

Dim binByte As Byte()
Dim myBinaryReader As BinaryReader = New
BinaryReader(theResponse.GetResponseStream)
binByte = myBinaryReader.ReadBytes(theResponse.ContentLength )
myBinaryReader.Close()
return binByte

Feb 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by just.starting | last post: by
3 posts views Thread by kelvin | last post: by
1 post views Thread by Paul | last post: by
6 posts views Thread by cyusman | last post: by
1 post views Thread by Muddasir | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.