Generate some xml in javascript, prompt user to save it Generate some xml in javascript, prompt user to save it javascript javascript

Generate some xml in javascript, prompt user to save it


How about this downloadify script?

Which is based on Flash and jQuery, which can prompt you dialog box to save file in your computer.

Downloadify.create('downloadify',{  filename: function(){    return document.getElementById('filename').value;  },  data: function(){     return document.getElementById('data').value;  },  onComplete: function(){     alert('Your File Has Been Saved!');   },  onCancel: function(){     alert('You have cancelled the saving of this file.');  },  onError: function(){     alert('You must put something in the File Contents or there will be nothing to save!');   },  swf: 'media/downloadify.swf',  downloadImage: 'images/download.png',  width: 100,  height: 30,  transparent: true,  append: false});


Using a base64 encoded data URI, this is possible with only html & js. What you can do is encode the data that you want to save (in your case, a string of XML data) into base64, using a js library like jquery-base64 by carlo. Then put the encoded string into a link, and add your link to the DOM.

Example using the library I mentioned (as well as jquery):

<html><head>    <title>Example</title></head><body>    <script>        //include jquery and jquery-base64 here (or whatever library you want to use)        document.write('<a href="data:application/octet-stream;base64;charset=utf-8,' + $.base64.encode( "this is a example, which requires the jquery-base64 library to work... replace this text with your xml" ) + '">click to make save dialog</a>');    </script></body></html>

...and remember to make the content-type something like application/octet-stream so the browser doesn't try to open it.

Warning: some older IE versions don't support base64, but you said that didn't matter, so this should work fine for you.


Without any more insight into your specific requirements, I would not recommend a pure Javascript/HTML solution. From a user perspective you would probably get the best results writing a native application. However if it will be faster to use Javascript/HTML, I recommend using a local application hosting a lightweight web server to serve up your content. That way you can cleanly handle the file saving server-side while focusing the bulk of your effort on the front-end application.

You can code up a web server in - for example - Python or Ruby using very few lines of code and without 3rd party libraries. For example, see:

"""Serves files out of its current directory.Doesn't handle POST requests."""import SocketServerimport SimpleHTTPServerPORT = 8080def move():    """ sample function to be called via a URL"""    return 'hi'class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):    def do_GET(self):        #Sample values in self for URL: http://localhost:8080/jsxmlrpc-0.3/        #self.path  '/jsxmlrpc-0.3/'        #self.raw_requestline   'GET /jsxmlrpc-0.3/ HTTP/1.1rn'        #self.client_address    ('127.0.0.1', 3727)        if self.path=='/move':            #This URL will trigger our sample function and send what it returns back to the browser            self.send_response(200)            self.send_header('Content-type','text/html')            self.end_headers()            self.wfile.write(move()) #call sample function here            return        else:            #serve files, and directory listings by following self.path from            #current working directory            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler)print "serving at port", PORThttpd.serve_forever()

Finally - Depending on who will be using your application, you also have the option of compiling a Python program into a Frozen Binary so the end user does not have to have Python installed on their machine.