Mark A. Nicolosi
Mark A. Nicolosi

Reputation: 85571

Download Javascript generated XML in IE6

I'd like to use Javascript to make IE6 download a file. It'll be created on the fly using Javascript. This file doesn't exist on a webserver. Here's a small example:

<!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" xml:lang="en" lang="en">
  <head>
    <script type="text/javascript">
function clicked() {
  var xml = "<data>Just for testing</data>";
  document.open("text/xml", "replace");
  document.write(xml);
}
    </script>
  </head>
  <body>
    <input type="button" value="Download" onclick="clicked();" />
  </body>
</html>

Instead of loading the xml in the browser window, I want it to cause IE6 to prompt the user where to download the data to so that it can be saved without them having to use File -> Save as. Any ideas?

Upvotes: 2

Views: 700

Answers (3)

Luca Matteis
Luca Matteis

Reputation: 29267

No, this is not possible. A web-browser strictly doesn't allow this, as the ability to save files to disk through JavaScript only, would be very dangerous, even if the confirmation popup shows up.

EDIT: Thanks to other answers, I found out (not surprisingly) that this behavior is possible with some versione of IE.

Upvotes: -1

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171411

If your data must be generated client side, then you can post it back to the server so that it can be returned as a downloadable file.

Upvotes: 0

Matt Bridges
Matt Bridges

Reputation: 49385

For IE6 you should be able to use document.execCommand() after your document.write():

document.execCommand('SaveAs',true,'file.xml');

This is not part of any standard and will only work in IE flavor browsers.

Upvotes: 2

Related Questions