Solved

ASP page executing twice when ContentType="application/pdf"

Posted on 2001-06-19
12
920 Views
Last Modified: 2012-08-14
We are trying to read a .pdf file from a file server and return it to a new browser window.  Which .pdf file we choose depends on some form fields.  But I have stripped out all the extraneous stuff from these two files to illustrate the problem.  First, here is the form:

  <HTML>
  <BODY onload='document.form1.submit();'>
  PDF TEST!
  <FORM name="form1" method="post" action="pdfDisplay.asp" target="newWin">
   <INPUT type='hidden' name='myfield' value='x'>
  </form>
  </body>
  </html>


Here is the asp page:


  <%
    stop   'for debugging

    Response.Buffer = TRUE

    Response.ContentType="application/pdf"

      Dim vntStream

      'Set oMyObject = Server.CreateObject("BinReadObject.BinRead")

      'create the binary stream

      'vntStream = oMyObject.readBinFile("F:\Data\Sample.pdf")
      'Response.BinaryWrite(vntStream)

    Response.Flush
    Response.End
  %>


Note that the code for reading and writing the .pdf file is commented out.  When we submit the form as it is above, the debugger executes the asp page twice thru ... first hitting the "stop" command and then stepping thru to the response.end, then it inexplicably starts over again and repeats one time.  

However, if we comment out the response.contenttype setter, it only executes once.  We could normally live with this except that on the second time thru the Request object has no form data in it and so we cannot determine what the user is requesting.  Also, the buffering and flushing does not appear to make any difference.

Has anyone seen this weirdness before?  If so, how did you resolve it?

0
Comment
Question by:knightEknight
12 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 6206628
Take out the onLoad() call in the body tag...
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6206833
why?  then the form won't submit.  no dif between this and hitting a submit button.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6207309
Yes there is, when you are submitting the form to the page, it is going to process it twice, once for the <form> and another on the onLoad(), (or something like that), try this instead;


<HTML>
<script language="javascript">
function doTheForm();
{
   document.form1.submit();
}
</script>
 <BODY>
PDF TEST!
 <FORM name="form1" method="post" target="newWin">
  <INPUT type='hidden' name='myfield' value='x'>
<input type="submit" onclick="doTheForm();">
 </form>
 </body>
 </html>

But I don't see where in your code the new browser window will open other than setting "target"?

If you want to pass a field data to a new window, try something like this;

<HTML>
<script language="javascript">
function doTheForm();
{
   theVal = document.form1.myField.value;
   window.open("pdfDisplay.asp?pagename="+theVal+");
}
</script>
 <BODY>
PDF TEST!
 <FORM name="form1" method="post">
  <INPUT type='hidden' name='myfield' value='x'>
<input type="submit" onclick="doTheForm();">
 </form>
 </body>
 </html>




0
 
LVL 33

Author Comment

by:knightEknight
ID: 6208145
Thanks, I do appreciate your effort, but the form and the ASP page are two seperate pages, so I must disagree with your assessment that the the onLoad is causing the double-execution.  If that were true, then why does commenting-out the ContentType setter in the ASP page cause it to only execute once?

Yes, the target="newWin" will open a new window (as long as there is no currently-opened window with that name)

Using the QueryString is a good suggestion also, but as I pointed out, there is nothing in the Request Object on the second iteration of the ASP page ... meaning no form data and no query-string data either.   :(
0
 
LVL 18

Accepted Solution

by:
mgfranz earned 100 total points
ID: 6208246
If I follow your question correctly, you want to select a .pdf file from a form select list, pass this to a new window/browser.  Yes?

Can you post the entire .asp displaying page
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6208653
We solved the problem by submitting the form as "get" instead of "post".  It still executes the ASP page twice, but the Request object is populated on both passes, so it will have to do.  Thanks!
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 18

Expert Comment

by:mgfranz
ID: 6208672
Interesting... I do something similar with .xls pages, dynamic creation and then open in a new window...

Have you thought of using a Response.Redirect()?

' Now open the newly created .xls file
Response.CacheControl = "no-store"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
Response.ContentType = "application/vnd.ms-excel"
Response.Clear
Response.Redirect "temp/"&theSessionID&".xls"

This could very easily be modified to open a passed in .pdf file with just adding Response.ContentType="application/pdf"

... maybe.
0
 
LVL 33

Author Comment

by:knightEknight
ID: 6210170
Thanks, I'll look into this.  The problem only occurs when that content-type is "application/pdf", but the Response.Redirect might be a way around this.
0
 

Expert Comment

by:satishjupalli
ID: 7469020
hi how come the last solution with response.redirect will show the result in new window.  
0
 
LVL 33

Author Comment

by:knightEknight
ID: 7469042
if you want it in the same window, then remove the target clause in the form ... that is, remove this:

target="newWin"
0
 

Expert Comment

by:IX9
ID: 8935823
On a slightly different tack...

I encountered this strange issue this morning as I was attempting to set up a click counter that increments by one each time a user clicks a link for a PDF document.  And apparently the ASP was executing not just twice, but three times!  So for each click I was registering three hits instead of one!  I was not able to find a "real" solution in the past two hours and don't have the time to waste all day looking for it.

So I have resorted to using a Session variable (they're not so bad as long as they're carefully controlled).

<%
   if Session("counted")<> "YES" then
     [-- execute counter increment routine --]
   end if
   Session("counted")="YES"
   Session.Timeout=1  '  set the session to timeout after the minimum period (1 minute)
   Response.ContentType="application/pdf"
   Response.Redirect("mypdfdocument.pdf")
%>

So the silly ASP can execute itself two, three, five, or ten times for all I care.  It doesn't matter to me if it's the best solution, only that it does what it's supposed to!
0
 
LVL 33

Author Comment

by:knightEknight
ID: 8938879
thanks ix9!  
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

19 Experts available now in Live!

Get 1:1 Help Now