tags:

views:

141

answers:

2

I am generating a XMl document via JavaScript and need to prompt the user to save it to their local machine.

This is the code I have at the moment:

var xmlWindow = window.open("", "");
xmlWindow.document.write("<node>data</node>");

This writes the xml to a new window which the user can then hit file->save to download.

  1. Is there any more elegent way of doing this?
  2. Is there any way to set the mimeType of the new window so the xml displays properly?
  3. is there any way to specify the title so when the user hits save, the file name is set correctly?

note: needs to be compatible with most brosers (IE6, IE7, FF, Chrome).

A: 

"save it locally": A tough call, if you want to be compatible across browsers. See this post.

TiddlyWiki manages to do this for some browsers (not sure which ones it supports) but only by having the user install certain files.

Jason S
I'm not actually trying to write it locally without the user's intervention. Just produce a 'save file' dialog box as if the file had come from a server.
alumb
+2  A: 

You could send the XML through an XMLHttpRequest to a script on your server to make the xml file, then set window.location to the location of your new xml file. They'll be prompted to save it.

seanmonstar
+1. Or generate the XML in the server to avoid a round trip.
Chetan Sastry
unfortunatlly, this is not possible. This has to be all client side. I don't control the server side.
alumb