Solved

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

Posted on 2007-11-27
14
537 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
ID: 20361287
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
ID: 20361415
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
ID: 20361457
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 20361491
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
ID: 20363171
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
ID: 31411288
thanks!!!  
0
 
LVL 1

Author Comment

by:dbedoo
ID: 20366186
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
 
LVL 18

Expert Comment

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

Author Comment

by:dbedoo
ID: 20366427
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
ID: 20366586
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
ID: 20367372
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
ID: 20367732
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
ID: 20367848
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
ID: 20368114
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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