Read PDF and Stream to Client

M

morebeer

Dear Experts,

I am trying to read a local PDF file using an aspx page and flush it
to the client. Basically my script works but the PDF sent flushed to
the client is broken because I am putting the stream into a string
object. But how do I flush the stream as a binary file? Here's my
code:

Stream lsStream = new FileStream(lsFile, FileMode.Open);
using (BinaryReader lrReadFile = new BinaryReader(lsStream))
{
lsCompletePage = lrReadFile.Read().ToString();
lrReadFile.Close();
}

lsStream.Close();

string strPdf = lsCompletePage;
Response.AddHeader("Content-Disposition", "attachment; filename="
+ Request.QueryString["pdf"].ToString());
Response.AddHeader("Content-Type", "application/pdf");
 
S

sloan

Untested and not sure (?)

But:

Page.Response.BinaryWrite(stm.ToArray());

stm is a stream of course.


Maybe that can help.
 
S

sloan

Interesting post:

http://forums.whirlpool.net.au/forum-replies-archive.cfm/970529.html



sloan said:
Untested and not sure (?)

But:

Page.Response.BinaryWrite(stm.ToArray());

stm is a stream of course.


Maybe that can help.
morebeer said:
Dear Experts,

I am trying to read a local PDF file using an aspx page and flush it
to the client. Basically my script works but the PDF sent flushed to
the client is broken because I am putting the stream into a string
object. But how do I flush the stream as a binary file? Here's my
code:

Stream lsStream = new FileStream(lsFile, FileMode.Open);
using (BinaryReader lrReadFile = new BinaryReader(lsStream))
{
lsCompletePage = lrReadFile.Read().ToString();
lrReadFile.Close();
}

lsStream.Close();

string strPdf = lsCompletePage;
Response.AddHeader("Content-Disposition", "attachment; filename="
+ Request.QueryString["pdf"].ToString());
Response.AddHeader("Content-Type", "application/pdf");
 
M

morebeer

Thanks for all your replies!
So, the easiest way to flush a binary (PDF) file is the following:

string lsFile = "test.pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" +
Request.QueryString["pdf"].ToString());
Response.AddHeader("Content-Type", "application/pdf");
Response.TransmitFile(lsFile);

This basically is the C# equivalent to PHP's readfile() and exactly
what I was looking for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top