Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Problem Downloading PDF with HTTPWebRequesst

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GJK - 14 Feb 2006 18:07 GMT
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.ReadToEnd()))
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?
tdavisjr - 14 Feb 2006 18:50 GMT
Dont set the Content-Disposition header.  Instead, set the
Response.ContentType = "application/pdf"

Everything else looks ok.
DKode - 14 Feb 2006 18:52 GMT
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
Joerg Jooss - 14 Feb 2006 21:03 GMT
Thus wrote GJK,

> Hello,
> I am trying to download a pdf file using HTTPWebRequest class and then
[quoted text clipped - 13 lines]
> 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.
[quoted text clipped - 5 lines]
> 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,
Signature

Joerg Jooss
news-reply@joergjooss.de

GJK - 15 Feb 2006 14:13 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.