Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 950
  • Last Modified:

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?

0
knightEknight
Asked:
knightEknight
1 Solution
 
mgfranzCommented:
Take out the onLoad() call in the body tag...
0
 
knightEknightAuthor Commented:
why?  then the form won't submit.  no dif between this and hitting a submit button.
0
 
mgfranzCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
knightEknightAuthor Commented:
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
 
mgfranzCommented:
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
 
knightEknightAuthor Commented:
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
 
mgfranzCommented:
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
 
knightEknightAuthor Commented:
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
 
satishjupalliCommented:
hi how come the last solution with response.redirect will show the result in new window.  
0
 
knightEknightAuthor Commented:
if you want it in the same window, then remove the target clause in the form ... that is, remove this:

target="newWin"
0
 
IX9Commented:
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
 
knightEknightAuthor Commented:
thanks ix9!  
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now