Link to home
Start Free TrialLog in
Avatar of knightEknight
knightEknightFlag for United States of America

asked on

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

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?

Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Take out the onLoad() call in the body tag...
Avatar of knightEknight

ASKER

why?  then the form won't submit.  no dif between this and hitting a submit button.
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>




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.   :(
ASKER CERTIFIED SOLUTION
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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.
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.
Avatar of satishjupalli
satishjupalli

hi how come the last solution with response.redirect will show the result in new window.  
if you want it in the same window, then remove the target clause in the form ... that is, remove this:

target="newWin"
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!
thanks ix9!