1

I have a function in javascript called "dumpData" which I call from a button on an html page as **onlick="dumpData(dbControl);"* What it does is return an xml file of the settings (to an alert box right now). I want to return it to the user as a file download. Is there a way to create a button when click will open a file download box and ask the user to save or open it? (sorta of like right-clicking and save target as)...

Or can it be sent to a php file and use export();? Not sure how I would send a long string like that to php and have it simple send it back as a file download.

Dennis

Dennis
  • 1,145
  • 4
  • 20
  • 35

3 Answers3

1

I don't think you can do that with javascipt, at least not with a nice solution.

Here's how to force a download of a file in PHP:

$file = "myfile.xml";
header('Content-Type: application/xml');
header("Content-Disposition: attachment; filename='$file'");
header('Content-Length: ' . filesize($file));
readfile($file);
exit;

Instead of using readfile to output your file, you could also directly display content using echo.

/EDIT: hell, someone was faster :).

EDITED:

just a proof of concept.. but you get the idea!

instead of

<a onlick="dumpData(dbControl); href="#">xml file</a>

you can have like this:

<a  href="foo/bar/generate_xml.php?id=1">xml file</a>

then like this:

// Assuming your js dumpData(dbControl); is doing the same thing, 
// retrieve data from db!

$xml = mysql_query('SELECT * FROM xml WHERE id= $_GET['id'] ');
header("Content-type: text/xml");
echo $xml;
Luca Filosofi
  • 30,905
  • 9
  • 70
  • 77
watain
  • 4,838
  • 4
  • 34
  • 35
  • the file is never created, its just the results of a function.. But I want to send it back to the user has if its a file to download.. – Dennis May 20 '10 at 19:57
  • @Dennis: watain is right! by using his way you don't need to create the file you just create a link for download! just delete the `$file = "myfile.xml";` and replace the `readfile($file);` with `echo $content_from_db` +1 – Luca Filosofi May 21 '10 at 23:45
0

POST the XML via a form to a php script that writes it back to the client with a Content-Disposition: attachment; filename=xxx.xml header.

<form name="xml_sender" action="i_return_what_i_was_posted.php" method="POST">
<input type="hidden" name="the_xml" value="" />
</form>

Then with js

function dumpData(arg) {
  var parsedXML = ??? //whatever you do to get the xml
  //assign it to the the_xml field of the form
  document.forms["xml_sender"].the_xml.value = parsedXML;
  //send it to the script
  document.forms["xml_sender"].submit();
}

Can't remember if this loses the original window, if so, post to an iframe.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Not sure what you mean... If I knew how, I wouldn't have spent the last 7.5 hours trying to figure it out with no luck! lol Its driving me insane and google searches are no help.. – Dennis May 20 '10 at 19:56
  • sending a post request using a form seems to be a good solution :). – watain May 21 '10 at 15:13
0

I eneded up going this route:

The HTML code

    <script type="text/javascript">

        $(document).ready(function() {

            $("#save").click(function(e) { openDialog() } );
            });


    </script>

<button id="save" >Send for processing.</button>

The javascript code:

function openDialog() {

    $("#addEditDialog").dialog("destroy");
    $("#Name").val('');
    $("#addEditDialog").dialog({
        modal: true,
        width: 600,
        zIndex: 3999,
        resizable: false,
        buttons: {
            "Done": function () {
                var XMLname = $("#Name").val();
                var XML = dumpXMLDocument(XMLname,geomInfo);
                var filename = new  Date().getTime();
                $.get('sendTo.php?' + filename,{'XML':XML}, function() {
                    addListItem(XMLname, filename + ".XML");
                });
                $(this).dialog('close');
            },
            "Cancel": function () {
                $("#Name").val('');
                $(this).dialog('close');
                //var XMLname = null;
            }
        }

    });

    }

PHP Code, I just decided to write the file out to a directory. Since I created the filename in the javascript and passed to PHP, I knew where it was and the filename, so I populated a side panel with a link to the file.

<?php    
    if(count($_GET)>0)
    {
        $keys = array_keys($_GET);
        // first parameter is a timestamp so good enough for filename    
        $XMLFile = "./data/" . $keys[0]  . ".kml";
        echo $XMLFile; 
        $fh = fopen($XMLFile, 'w');
        $XML = html_entity_decode($_GET["XML"]);
    $XML = str_replace( '\"', '"', $XML );
        fwrite($fh, $XML);
        fclose($fh);        
    }   
    //echo "{'success':true}"; 
    echo "XMLFile: ".$XMLFile;
?>

I don't know why, but when I send the XML to my php file it wrote out the contents withs escape charters on all qoutes and double quotes. So I had to do a str_replace to properly format the xml file. Anyone know why this happens?

Dennis
  • 16