How to load a JavaScript string as a file

The application requests KML data via AJAX from the server. This data is stored in javascript variables and displayed in the Google Earth plugin.

In javascript, how can I provide a link to load KML data stored in a javascript variable (as a string) without querying the server?

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

suggests using a data URI, but that probably won't work in enough browsers for my needs. It's probably easier to just go back to the server to get the data again for download, but it is curious if someone pulled it using javascript.

+4
source share
7 answers

Short answer: you cannot and are still platform independent. Most browsers simply do not allow javascript to manipulate the file system.

However, you could get away with some hackers for a particular platform. For example, IE offers an execCommand function that you can use to call SaveAs. If you did this in an IFrame that had data that you wanted to save, you can make it work, but only in IE. Other options (again, I'm going to install Microsoft here) include this Silverlight hack or ActiveX controls.

I think that for full compatibility with the platform you just have to suck it and provide a server-side boot option.

[Change] Oops! When I went looking for links, I did not do due diligence. It turns out that the Silverlight hack I'm connected to has a server component. Sounds like you're pretty SOL.

[Edit2] I found a good summary of browser compatibility for execCommand here . Although it lists question marks for the saveas command, this may be a good way for you. Worth a try, maybe?

[Edit3] Well, I decided to make a proof of concept of the approach that I proposed, and I have something pretty simple in IE. Unfortunately, I proved that this approach will not work for Firefox and does not seem to work in Chrome / Safari. So it is very platform dependent. But it works! Here is the full working page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <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> 
+3
source

Yes, I'm afraid you should pass it back to the server. Make a general "echo" script that spits out all the parameters that are served to it.

At least you can force download with the correct MIME type:

 "content-disposition","attachment; filename=data.xml" 
+2
source

You might want to check this out: it's called Downloadify . It uses a combination of Javascript and Flash and can save a string in almost any format. Try the demo and see for yourself!

+2
source

Check out http://regany.com/blog/2014/05/30/convert-a-string-to-a-download-file-in-javascript/

Enable popups and use the following code:

 var str = "the string you wan't to download"; window.open('data:text/plain,' + encodeURIComponent(str)); 
+2
source

Perhaps it would be useful (JSP variant):

  private void printSaveStringButton(String fileName, String content) throws Exception { //add new invisible container with write / save functions out.println("<iframe id=\"xmlContentId\" style=\"display:none;\"></iframe>"); //save string in js variable String jScript = "\n" + "var SaveHelper = {\n" + " content : null,\n" + " saveContent : function(filename, text) {\n" + " text=(SaveHelper.content!=null)?SaveHelper.content:text;\n" + " var doc = document.getElementById('xmlContentId').contentWindow.document;\n" + " doc.write(text);\n" + " doc.execCommand(\"saveAs\",true,filename);\n" + " doc.close();\n" + " }\n" + "};\n" + "SaveHelper.content = '" + org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(content) + "';\n"; out.println("<script type=\"text/javascript\">" + jScript + "</script>"); //add button that writes content into iframe container and show save dialog. out.println("<button type=\"button\" onclick=\"SaveHelper.saveContent('"+fileName+"' )\">Save as...</button>"); } 
+1
source

Perhaps you could use the parseKml function to parse kml data in a javascript variable, rather than trying to save it in a file and modify it from javascript (which I believe is not possible due to security reasons)

https://developers.google.com/earth/documentation/kml

+1
source

Honestly, I do not think this is possible. It was never planned that this could be done in javascript.

0
source

All Articles