views:

907

answers:

3

Application requests KML data through AJAX from server. This data is stored in javascript variables, and displayed in Google Earth plugin.

In javascript, how do I provide a link to download the KML data stored in javascript variable (as a string) without requiring a request back to server?

This link: http://forum.mootools.net/viewtopic.php?id=9728

suggests the use of data URI, but that probably won't work across enough browsers for my needs. Probably simplest just to go back to server to get data again for download, but curious if anyone has pulled this off with javascript.

+1  A: 

Frankly, I don't think this is possible. It was never intended that this could be done in javascript.

Georg
+2  A: 

Short answer: you can't and still be platform independent. Most browsers just don't allow javascript to manipulate the filesystem.

That said, you might be able to get away with some very platform-specific hacks. For example, IE offers the execCommand function, which you could use to call SaveAs. If you did that within an IFrame that had the data you wanted to save, you might get it working -- but only on IE. Other options (again, I'm going Microsoft specific here) include this Silverlight hack, or ActiveX controls.

I think for full platform compatibility, you're just going to have to suck it up and provide a server-side download option.

[Edit] Whoops! I didn't do enough due diligence when I went link-hunting. It turns out the Silverlight hack I linked to has a server-side component. Looks like you're pretty SOL.

[Edit2] I found a nice summary of browser compatibility for execCommand here. Although it lists question marks for the "saveas" command, maybe that might be a good route for you after all. Worth a try, perhaps?

[Edit3] Well, I decided to do a proof of concept of the approach I suggested, and I got something fairly simple working in IE. Unfortunately, I proved in the process that this approach will not work for Firefox and doesn't appear to work in Chrome/Safari, either. So it's very platform dependent. But it works! Here's a complete working page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Javascript File Saver</title>
    <script type="text/javascript">
      function PageLoad() {
        var fdoc = window.frames["Frame"].document;
        fdoc.body.appendChild(fdoc.createTextNode("foo,bar,baz"));
      }
      function Save() {
        var fdoc = window.frames["Frame"].document;
        fdoc.execCommand("SaveAs", true);
      }
    </script>
</head>
<body onload="PageLoad();">
<h2>Javascript File Saver</h2>
<iframe id="Frame" style="width: 400px;">Noframe</iframe><br />
<button onclick="Save();">Save</button>
</body>
</html>
Randolpho
But then in Firefox you can use data urls with the mime type `application/octet-stream`. This mime type basically means "binary" and the only thing Firefox will do with such is to provide a Save As dialog.However, there is still Chrome missing for me.
panzi
@panzi: thanks for the input. Soon as I get a chance I'll try to check that out.
Randolpho
A: 

Yeah, I'm afraid you have to pass it back to the server. Make a generic "echo" script that spits out whatever parameters are fed to it.

At least you can force a download with the right MIME type:

"content-disposition","attachment; filename=data.xml"
Diodeus