469,353 Members | 2,167 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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 28072
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ivan Demkovitch | last post: by
reply views Thread by Frank Jeseit | last post: by
6 posts views Thread by john | last post: by
5 posts views Thread by Simon Rigby | last post: by
10 posts views Thread by =?Utf-8?B?SnVhbg==?= | last post: by
2 posts views Thread by Ron Hinds | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.