473,769 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
ResponseStreamR dr = New StreamReader(th eResponse.GetRe sponseStream(),
New UnicodeEncoding )
Dim objMemoryStream As New MemoryStream(Ne w
UnicodeEncoding ().GetBytes(Res ponseStreamRdr. 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_conte nt"];
Response.AddHea der("Content-Disposition","a ttachment;filen ame=" +
fileName);
Response.Output Stream.Write(bt eContent, 0, bteContent.Leng th);
Response.Flush( );
Response.End();

Any idea what is wrong with the code?

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

Feb 14 '06 #1
4 1473
Dont set the Content-Disposition header. Instead, set the
Response.Conten tType = "applicatio n/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()
ResponseStreamR dr = New
StreamReader(th eResponse.GetRe sponseStream(),
New UnicodeEncoding )
Dim objMemoryStream As New MemoryStream(Ne w
UnicodeEncoding ().GetBytes(Res ponseStreamRdr. ReadToEnd()))
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_conte nt"];
Response.AddHea der("Content-Disposition","a ttachment;filen ame=" +
fileName);
I'm nitpicking, but use AppendHeader(). AddHeader() is an ASP legacy API.
Also, set the HTTP Content-Type header for PDF:

Response.Conten tType = "applicatio n/pdf";
Response.Output Stream.Write(bt eContent, 0, bteContent.Leng th);
Response.Flush( );
Response.End();
Any idea what is wrong with the code?
No, that looks fine.
Note: One thing I noticed during debugging:
theResponse.con tentlength=2206 9 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********@joer gjooss.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(th eResponse.GetRe sponseStream)
binByte = myBinaryReader. ReadBytes(theRe sponse.ContentL ength)
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
2083
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 any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
5
5970
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) for it. We use this control on an ASP page. We put the CAB file for the AX control and VBRun60.CAB in the same folder with all the ASP files. When we call the ASP page using any machine with IE 6 SP2 (we tried it with multiple IE6 SP2...
0
1638
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 downloading with the error message: "the source file could not be read". And the error is not dependent on the size of the file, as I can download a file of 6MB but having problem with a file with 652KB. It always stops after 666880bytes. Thanks.
102
6821
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 never to send if-modified-since. But the strange thing is that Opera sends if-modified-since but when I reply with "HTTP/1.0 304 Not Modified" it is not refreshing the screen from its cache, it is leaving the screen blank.
3
2358
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 Downloading package: Console_Getopt-stable....ok Downloading package: XML_RPC-stable....ok Downloading package: HTML_Template_IT-stable....ok Downloading package: Net_UserAgent_Detect-stable....ok
1
2064
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 firewall. But as I can downloaded eggs manually, I'm not overly concerned at this point. So I've tried following the instructions for un-networked installs: I've downloaded ez_setup.py and setuptools-0.6c3-py2.4.egg, putting the egg file in the same...
1
1659
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 Using local package: Archive_Tar-stable....ok Using local package: Console_Getopt-stable....ok Using local package: XML_RPC-stable....ok Bootstrapping: PEAR...................(remote) <!DOCTYPE html PUBLIC
6
3766
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 .NET 1.1, IIS 6, Win2003. Now we are having problem when trying to download word documents located in the file server. It often says Internet Explorer cannot download <filenamefrom <servername>. Sometimes we are able to view
1
1686
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 been created on the server.once created then user can download that file. i am able to download file but the problem is that when user apply sorting to data and then save data in excel sheet. the file on the server contains the data in the same...
1
3535
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 refresh.redirect() occure it give me error that can't redirect because headers have been sent following are my code to download file
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.