Streaming PDF to Browser from Server

R

Rob

I have an ASP.Net web app that generates a Crystal Report in PDF format.
Since there is sensitive data within the reports, the pdfs need to be
streamed to the browser then deleted immediately. The report gets kick off
by a Java application redirecting the URL to the ASP.Net app on the IIS
Server with all report selection criteria appended to the querystring. The
Crystal Report is rendered in a new window.

In the Page_Load() event, I pull down all of the querystring parameter
values, use the values to query the database, return a dataset, generate a
Crystal Report in PDF Format, stream the PDF to the browser, then delete the
PDF. This works all fine and dandy, but the Page_Load() event gets executed
twice, so everything previously stated gets executed again. I am using IE
5.5. This issue is covered by Microsoft Article 307603.

http://support.microsoft.com/default.aspx?kbid=307603

What I was wondering is if any other developers have experienced this same
issue and what type of coding techniques were used to get around querying
the database twice and only streaming and deleting the PDF once?

If you have a PDF file, execute the following code below and put a
breakpoint in the code. You will see that this event is executed 2 or 3
times, depending on the version of your browser. No postbacks are made.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Query Database Code
...

'Generate Crystal Report in PDF Format
...

'Stream PDF to Browser
'Set the appropriate ContentType.
Response.ContentType = "Application/pdf"
'Get the physical path to the file.
Dim FilePath As String = MapPath("acrobat.pdf")
'Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()

End Sub


Any help or comments are appreciated.

Thanks,
Rob
 
M

Michael Pearson

To get around the "page loading twice issue" I redirect the user to a PDF
stored on disk. That seems to solve that problem, but doesn't solve the
"PDF must be deleted immediatly" problem.

Maybe you can have a Service that checks that directory and deletes any
files older than X (maybe one minute)?
 
H

Hans Kesting

Rob said:
In the Page_Load() event, I pull down all of the querystring parameter
values, use the values to query the database, return a dataset, generate a
Crystal Report in PDF Format, stream the PDF to the browser, then delete the
PDF. This works all fine and dandy, but the Page_Load() event gets executed
twice, so everything previously stated gets executed again. [snip]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
[snip]

What probably happens: in vb.net there is a feature that automatically
connects
events to methods. A method with the name Page_Load is then automatically
connected to the Load event of the Page.
In this case you also specify that connection explicitly (Handles ..), so
the method
is connected twice, and thus fired twice.

Solution: either switch that autoconnect feature off, or remove the
"handles" clause.


Hans Kesting
 
J

Jerry Boone

I don't think your going to get around the problem of having the PDF not
accessible by the client for saving locally.

But... what about making your report a web service? I haven't done it yet
but from what I understand... you can right click and "Publish as web
service", then build a small little windows application that includes the cr
viewer and a reference to the web service. It might possibly give you the
control your seek... even the ability to deny printing the report.

--
Jerry Boone
Analytical Technologies, Inc.
http://www.antech.biz
Secure Hosting and Development Solutions for ASP, ASP.NET, SQL Server, and
Access
 

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