Read PDF and Stream to Client

  • Thread starter Thread starter morebeer
  • Start date Start date
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");
 
Untested and not sure (?)

But:

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

stm is a stream of course.


Maybe that can help.
 
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");
 
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.
 
Back
Top