Creating PDFs dynamically forces page reload

G

Guest

Hi all,

Im having difficulty serving synamically generated PDFs to clients via
asp.net. Im using crystal reports to generate them. In my page load I have
the following:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
try
{
ReportDocument rd = new ReportDocument();
rd.Load(file);
for (int i = 1; i < Request.QueryString.Count; ++i)
{
//setup report parameter
}
MemoryStream oStream = null; // using System.IO
oStream =(MemoryStream)rd.ExportToStream

(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.ContentType = "application/pdf";
Response.Buffer= true;
Response.BinaryWrite(oStream.ToArray());
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message, ex);
}
Response.End();
}

Although this works fine, Page_load gets called twice before the pdf is
served to the client. Any ideas why this is? For a time intesive reoprt its
pretty critical that its only run the once!

Some notes:

If the Response.BinaryWrite call is commented out, page_load is only called
once, however I get a blank page (as expected).

On the other hand, if Response.End is commented out, then the pdf is served
as a download ("do you wish to open or save etc") with the aspx page as the
default filename - saving this as a pdf results in the report. Again,
page_load is only called the once.

Keeping both calls in displays the pdf correctly, but only after the second
call to page_load. There is a screen reload between the two calls though.

Thanks in advance,

Shak
 
B

bruce barker

when IE it loads an file that requires a viewer object (like pdf) it may
request more than once. Older versions of IE will actually do 3 requests.

when the browser get a pdf response, it knows it must load acrobat reader,
so it checks again with a request to only fetch the mime-type (of course if
your page doesn't check the request header, it send the whole response.)
after validation the mime-type, it loads acrobat, then gives acrobat the url
of the pdf. acrobat then downloads the file and displays. with more recent
versions of IE, an attempt is made to keep the file in the download cache to
not bother with extra downloads, but does not always work out.

-- bruce
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top