Solved

httphandlers to open a pdf in adobe reader instead of using response.binarywrite  from the caller

Posted on 2007-11-27
14
535 Views
Last Modified: 2013-11-26
Hi experts,
   I have an application that is a web based crystal reports generator written in .net 2005.  My question is I want to be able to read the pdf in as stream and have it open Adobe Reader but still render the the same "generator" content on the aspx page.  I know an alternative solution would be open a new window from the aspx and open the pdf direct in IE  using Response.ContentType = "application/pdf"
BUT my project requirements are to open Adobe reader.  I can get this functionality directly from the calling aspx page using the following code:

s = New FileStream("somepdf", FileMode.Open, FileAccess.Read)
                        ReDim Preserve ba(s.Length)
                        s.Read(ba, 0, s.Length)
                        s.Close()
                        s.Dispose()
                        Response.BinaryWrite(ba)
                        'Response.Clear()
                        Response.End()

but as stated above I want the calling aspx page's content to remain or at least be re-rendered.  When using this method I get a blank .aspx page and abobe reader opens.  From what I read about httphandlers they could potentially solve this problem but I have no experience with them and I don't know where to begin.  Every example I have looked at is very specific to the task at hand...  none of which is close to my needs..  So is this a possible solution to my problem, and if so how do I begin?  Otherwise I am open to other suggetions..

Thanks in advance

Dave
0
Comment
Question by:dbedoo
  • 8
  • 6
14 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
I'm not sure I understand the question:
    You can open up Adobe reader by itself okay.
    You can do a normal page okay.
    You can't do a normal page and Adobe reader with the same request?

If my summary is correct,  then what you could do is make your normal page run some javascript that opens a 2nd window that will then request the pdf page independantly.  Or it might be as simple as having the target for the link for your pdf link set to "_blank".
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
Ok that sounds wonderful in theory!  when I try something like :

Me.Controls.Add(New LiteralControl("<script>window.open('reportHelper.aspx?fileName=" & reportInfo(3) & "', '_blank')</script>"))

it still opens a the reportHelper.aspx page blank...  If there was a way to make the page "process"  but only open adobe reader this would be a VERY easy fix..  

Is there a way?
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
sorry if Im not describing this clear enough here is some pseudo to explain the process

report generator(aspx page)  load
generate report button click
generate report
call reportHelper aspx page to handle stream

response.binary write stream

adobe opens with valid report but also opens a blank reportHelper page

0
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
Comment Utility
Ok, I understand you now.  Yes, an httphandler is perfect for this.  Just add one to your project (Choose "Add New Item..." and then look for "Generic Handler") and you should get a .ashx file.  Let's say you name it reportHelper.ashx.  Then you can use the content type and code from your original post above in the ProcessRequest() method for the handler (there should already be a basic "Hello World" example you can replace) and your javascript/literal control will be almost identical; just change reportHelper.aspx to reportHelper.ashx.
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
I tried it nd it worked like a charm... Every sample I found for httphandlers required a line theweb.config to make it work.  In this case I didnt need to worry about that..  I guess I'll have do some more research on all of the different ways to use them.  Thanks for your help!!  
0
 
LVL 1

Author Closing Comment

by:dbedoo
Comment Utility
thanks!!!  
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
One more thing though ... currently Im using
Me.Controls.Add(New LiteralControl("<script>window.open('reportHelper.ashx?fileName=" & reportInfo(3) & "' )</script>"))

but it still opens an addition blank window ...  which I didn't think possibe with this extention...

is there an alternative way of loading the .ashx without opening a window such as :

Me.Controls.Add(New LiteralControl("<script src='reportHelper.ashx?fileName=" & reportInfo(3) & "' ) type='text/javascript'></script>"))

this method is not working though ..    any suggestions?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
Try this:
Me.Controls.Add(New LiteralControl("<a href=""reportHelper.ashx?filename=" & reportInfo(3)  & """ target=""_blank"""))
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
did you mean for that to have a closing tag?

Me.Controls.Add(New LiteralControl("<a href=""reportHelper.ashx?fileName=" & reportInfo(3) & """ target=""_blank""></a>"))

I tried this but it didn't do anything -  Im guessing because it will render the link but it is not clicked therefore doesn't execute?

0
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
Yes, I did mean for it to have a closing tag, and you're right- it won't be clicked so it won't work.  

Looking again at the original javascript code, I'm thinking it should open one new window, and that window will contain your pdf file.  I've noticed firefox sometimes opens an extra window for downloads like this, but that's a firefox issue.  If you're getting two windows from the one request in IE, I don't know how to fix it.  If the link from your generator page is returning a blank aspx page that only opens the new pdf popup, then bypass the apsx page and move the report generation code to the handler as well, and make the link on the generator page run the javacript to open a new window.
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
well the problem is I only want the .ashx file to LOAD  not OPEN  because the pdf is opened in reader not ie or ff.  I just wonder if there is some way to only perform a load without opening it in a window.  For instance I have a javascript function in another app that used DOM  for XML and I can do something like.  
xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.load("place url here")

Well I have tried
Me.Controls.Add(New LiteralControl("<script>window.open('reportHelper.ashx?fileName=" & reportInfo(3) & "' )</script>"))
Me.Controls.Add(New LiteralControl("<script>window.close('reportHelper.ashx?fileName=" & reportInfo(3) & "' )</script>"))

that prompts me "Do you really wanna close this window"  Cant have that


Also Tried this
Me.Controls.Add(New LiteralControl("<a id=""httpHandler1"" href=""reportHelper.ashx?fileName=" & reportInfo(3) & """ target=""_blank""></a>"))
Me.Controls.Add(New LiteralControl("<script>document.getElementById('httpHandler1').click()</script>"))

via a forced button click ... I still get the back page..
0
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
If you download the pdf via a link in your web browser, it will open in a reader embedded in your browser.  Rather than using "window.open" perhaps changing "document.href.location" would be better?
0
 
LVL 1

Author Comment

by:dbedoo
Comment Utility
The problem is I am required to have it open directly in adobe reader, and not the browser.  If I could use the the IE adobe  my problem would be solved by simply adding
context.Response.ContentType = "application/pdf" in my ashx file because then the pdf opens directly in the ie window.  

when this line is taken out it opens in adobe reader application but also opens that extra ie window.
0
 
LVL 18

Expert Comment

by:jcoehoorn
Comment Utility
Technically, it is open inside Adobe reader, which is in turn hosted inside the browser.  There's really not any difference as far as the document is concerned.  If you check the running processes you'll be able to see that adobe reader is indeed running.  

I don't think it's possible to force it to open adobe reader (or any program) outside of the browser unless you take advantage of an activeX object or other special browser extension like you mention above.  Web browsers are specifically designed for security reasons to put web content into a sandbox, with only limited exposure to the computer as a whole.  Any web site that lets you view pdf files already takes advantage of such a special extension in the form of the Adobe reader browser extension.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now