473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1451
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
5
by: fniles | last post by:
We created an ActiveX control and marked it as safe for scripting using Implements IObjectSafety. We then created a CAB file and signed it using Verisign. We also created a license file (LPK file)...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
3
by: kelvin | last post by:
Can anyone tell me what could cause the error below? Starting installation ... Loading zlib: ok Downloading package: PEAR-stable......ok Downloading package: Archive_Tar-stable....ok...
1
by: alex23 | last post by:
Hey everyone, I'm trying to install setuptools on a work PC behind an NTLM firewall. I've tried to use APS as recommended but am still unable to have anything other than IE talk through...
1
by: Paul | last post by:
I just installed php 4.4 with apache 2.0 on WIN XP. All is working so far. From: the prompt, I type: go-pear and here's what I get: Loading zlib: ok Using local package: PEAR-stable......ok...
6
by: cyusman | last post by:
Hi, We have just moved our application to a new webfarm server environment which utilizing hardware load balancing, SSL off-loading and HTTP compression off-loading.My application is running on...
1
by: Muddasir | last post by:
Hello everyone. I am facing problem in downloading .xls file. I generate report and save data in excel sheet on server. and once user click the 'save data in excel format', an excel sheet is...
1
by: shahidrasul | last post by:
i want to download a file which user select from gridview, downloading is completing without problem but after download i want to refresh my page because i do some changes in db . but when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.