Open a new window with POST method to download a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dreamfalcon
    New Member
    • Oct 2007
    • 20

    Open a new window with POST method to download a file

    Hi!
    I have an AJAX function that send’s a string by the post method and display the result in the same page:
    [CODE=javascript]
    xmlHttp.onready statechange = stateChanged;
    xmlHttp.open('P OST', url, true);
    xmlHttp.setRequ estHeader("Cont ent-type", "applicatio n/x-www-form-urlencoded");
    xmlHttp.setRequ estHeader("Cont ent-length", parameters.leng th);
    xmlHttp.setRequ estHeader("Conn ection", "close");
    xmlHttp.send(pa rameters);
    //.....
    document.getEle mentById(id).in nerHTML=xmlHttp .responseText;[/CODE]

    But now I want another function that send’s the same string to the server (PHP), and the result is an Excel file, that it should display the download dialog.

    Any ideas?

    Thx in advance
    Last edited by gits; Oct 15 '07, 10:43 AM. Reason: added code tags
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    Originally posted by dreamfalcon
    But now I want another function that send’s the same string to the server (PHP), and the result is an Excel file, that it should display the download dialog.
    I am not sure I follow. You want to be able to send the same URL string to a server, but be able to get back two different results? The server side logic has to be able to differentiate between the two requests. If both strings are the same it will not know when to send HTML to your current window, or when to send the Excel file to a new window.

    If on the other hand all you want to do is pass a request back that will down load a file, it works the same way it would any other time. Just pass the URL. I dont think you want to use a popup window though. They are almost always blocked now.

    It would be safer to use an iFrame. You just add the tags and when you are ready just set the src attribute to the URL you want to pass. The other nice thing about this is that you can set the iFrame to be 1px X 1px so the user does not even have to see it.

    [HTML]<IFRAME id="downloadFil e" ></IFRAME>

    <script type="text/javascript">
    document.getEle mentById('downl oadFile').src=' <<Your URL HERE>>';
    </script>[/HTML]

    If you want to force the save dialog box, so they can not automatically open the file, then just set the return MIME type to an invalided value. ("applicatio n/unknown")
    Last edited by gits; Oct 15 '07, 10:44 AM. Reason: added code tags

    Comment

    • dreamfalcon
      New Member
      • Oct 2007
      • 20

      #3
      Originally posted by pronerd
      I am not sure I follow. You want to be able to send the same URL string to a server, but be able to get back two different results?
      The server scripts are different. One for the tables that are displayed, and one for the excel file, both process the same string, but with different results.

      Originally posted by pronerd
      <IFRAME id="downloadFil e" ></IFRAME>
      I tried:
      [CODE=javascript]
      xmlHttpExcel.op en('POST', url, true);
      //...
      document.getEle mentById('downl oadFile').src=x mlHttpExcel.res ponseText;
      [/CODE]
      and in the excel.php:

      [CODE=php]
      $hoje=date("Y_m _j");
      header("Content-type: application/x-msdownload");
      header("Content-Disposition: attachment; filename=".$tab le."_".$hoje.". xls");
      header("Pragma: no-cache");
      header("Expires : 0");
      print "$header\n$data ";
      [/CODE]
      But still cant pass the string by post method for the php file :(
      Last edited by gits; Oct 15 '07, 10:47 AM. Reason: added code tags

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Originally posted by dreamfalcon
        The server scripts are different. One for the tables that are displayed, and one for the excel file, both process the same string, but with different results.
        So how does the server know which script to use if you are passing the exact same URL?


        Originally posted by dreamfalcon

        [CODE=javascript]
        xmlHttpExcel.op en('POST', url, true);
        //...
        document.getEle mentById('downl oadFile').src=x mlHttpExcel.res ponseText;
        [/CODE]
        First off, why are you using AJAX for this call. It does not look like their is any purpose for it. It looks like you already have the URL. All you need to do is to set the src attribute to be the url value you already have. Instead it looks like you are setting the src attributre to be the response header text.

        If I am following what you want all you should need is

        [CODE=html]
        <iframe id="downloadExc elFile" ></iframe>
        [/CODE]
        [CODE=javascript]
        document.getEle mentById('downl oadFile').src=u rl;
        [/CODE]




        Originally posted by dreamfalcon

        [CODE=php]
        $hoje=date("Y_m _j");
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=".$tab le."_".$hoje.". xls");
        header("Pragma: no-cache");
        header("Expires : 0");
        print "$header\n$data ";
        [/CODE]
        I am not a PHP guy, but I do not see anything there that would generate an Excel file. That just looks like it is setting the MIME type for the HTTP response.


        Originally posted by dreamfalcon
        But still cant pass the string by post method for the php file :(
        How do you know the URL is not being passed? As soon as you set the src attribute of the iFrame a request will be sent to the URL you passed to it.

        Comment

        • dreamfalcon
          New Member
          • Oct 2007
          • 20

          #5
          Originally posted by pronerd
          So how does the server know which script to use if you are passing the exact same URL?
          the url is not the same, just the string that is passed by the POST method, I use diferent PHP scripts.

          Originally posted by pronerd
          First off, why are you using AJAX for this call. It does not look like their is any purpose for it. It looks like you already have the URL.
          I have the url of the script, but I don’t know how to pass the string for the script without using Ajax. The string is build using javascript.

          Originally posted by pronerd
          [CODE=javascript]
          document.getEle mentById('downl oadFile').src=u rl;
          [/CODE]
          Where I pass the string to the PHP script with the POST method? I can’t use GET

          Originally posted by pronerd
          I am not a PHP guy, but I do not see anything there that would generate an Excel file. That just looks like it is setting the MIME type for the HTTP response.
          I didn’t show all the code, just the final part. But it generate fine, this is not the problem.

          Let me make the question again. How can I pass a string by the POST method using JavaScript to a PHP file and show a download file dialog without change the page?
          Thx

          Comment

          • pronerd
            Recognized Expert Contributor
            • Nov 2006
            • 392

            #6
            Originally posted by dreamfalcon
            I have the url of the script, but I don’t know how to pass the string for the script without using Ajax. The string is build using javascript.
            Ok, I think I see the confusion here. I think what you mean to say is that you want to pass a parameter, and your server side listener can only accept an HTTP POST Request.

            So the easy way to do that would be to use the iFrame as I mentioned, and have it submit an HTML form element. That will pass the string you generate as an HTTP parameter in an HTTP POST REQUEST.


            Code:
            <FORM id="formSubmit" action="urlOfYourPHP.file" >
                <INPUT id="StringToSubmit" name="paraterName" value="valueToPassHere"  >
            </FORM>
            <script type="text/javascript">
                var formObj = document.getElementById("formSubmit");
                formObj.submit();
            </script>

            Comment

            Working...