Solved

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

Posted on 2001-06-19
12
926 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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/…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 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