472,334 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Stream pdf to Response

DC
Hi,

I am trying to load a pdf file into a memorystream and upload this file
to the browser. However, wenn I run the attached code (the ftpStream is
a valid stream of the pdf file) I receive an "Application error" in the
Adobe Reader. I tried all the default encodings and the windows
encoding (below) with the same result.

TIA for any hint on what could be wrong here.

Regards
DC

ftpStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";
byte[] buffer = new byte[8192];

int bytesRead = ftpStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
char[] charArray =
Encoding.GetEncoding("windows-1252").GetChars(buffer, 0, bytesRead);
Response.Write(charArray, 0, charArray.Length);

bytesRead = ftpStream.Read(buffer, 0, 8192);
}
Response.End();

Feb 6 '06 #1
3 28242
DC,

Why are you changing the bytes from the stream into characters? There
is no reason to do this. Just send the bytes as they are to the client.
You are changing the format of the file into something that Acrobat can't
read (by converting to characters).

Also, you can't be sure that you actually read all of the bytes from the
stream. Jon Skeet has a good article on this:

http://www.pobox.com/~skeet/csharp/readbinary.html

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"DC" <dc@upsize.de> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

I am trying to load a pdf file into a memorystream and upload this file
to the browser. However, wenn I run the attached code (the ftpStream is
a valid stream of the pdf file) I receive an "Application error" in the
Adobe Reader. I tried all the default encodings and the windows
encoding (below) with the same result.

TIA for any hint on what could be wrong here.

Regards
DC

ftpStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";
byte[] buffer = new byte[8192];

int bytesRead = ftpStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
char[] charArray =
Encoding.GetEncoding("windows-1252").GetChars(buffer, 0, bytesRead);
Response.Write(charArray, 0, charArray.Length);

bytesRead = ftpStream.Read(buffer, 0, 8192);
}
Response.End();

Feb 6 '06 #2
You might also want to have a look at the server.transfer() method.
It's pretty useful, you know.

Feb 6 '06 #3
DC
Thank you for the excellent advice Nicholas, I got it to work with
using byte arrays (see code below).

Regards
DC

byte[] buffer = new byte[8192];

ftpStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";

int bytesRead = ftpStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
byte[] buffer2 = new byte[bytesRead];
System.Buffer.BlockCopy(buffer, 0, buffer2, 0, bytesRead);

Response.BinaryWrite(buffer2);
Response.Flush();

bytesRead = ftpStream.Read(buffer, 0, 8192);
}
Response.End();

Feb 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ivan Demkovitch | last post by:
Hi! I'm using output stream to output image to webpage. //Move array to response stream. Response.ContentType = "image/jpeg";...
0
by: Frank Jeseit | last post by:
Hi, I call methods of my web service asynchronously. A few of my users report the exception below System.ArgumentNullException Message: value...
6
by: john | last post by:
The standard method to transmit a file from an aspx page to a browser is to stream the file to the response then end the response. The HTML code...
7
by: eventuranza | last post by:
For my application, there users can upload and download files to the a webserver. Straightforward enough. However, when they upload a PDF file...
5
by: Simon Rigby | last post by:
Hi folks, Apologies if this is not directly relevant but I was struggling to find a group with the appropriate context. So as my problem is in an...
1
by: mattridings | last post by:
Hi gang, Have a script that works fine. However, it's really cpu intensive and I'm looking for suggestions on a) whether or not that's normal...
5
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, The following code was suggested by one of the users in this newsgroup when a pdf file was requested by a user from an asp page. I used the...
2
by: Michael D. Ober | last post by:
When I single step through the code below, it sends back the PDF file that is retrieved in the line fm.GetAccountPDF(...). When I run without...
10
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see...
2
by: Ron Hinds | last post by:
I'm getting this in an ASP application on IIS6/W2K3. The page in question is trying to return a XML file approximately 45MB in size. Changing this...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.