3

I have an html site with a form in it and I want the user to be able to create a text/xml file depending on the input. But I wan't to avoid setting up a webserver only for this task.

Is there a good way, to do that, e.g. with Javascript? I think you can't create files with Javascript, but maybe create a data url and pass the text, so the user can save it to file?

Or is there another way to achieve this simple task without a webserver?

Baris Akar
  • 4,895
  • 1
  • 26
  • 54
  • Don't you already have a webserver? Where is your site running from? – nnnnnn Jul 29 '11 at 00:42
  • No, the html site is generated by a process on a server, but there is no apache installed, and I want to avoid installing it. The user would be able to get the html site from the server and display it offline in his browser. – Baris Akar Jul 29 '11 at 16:28

2 Answers2

1

Solved it, somehow. I create a data url data:text/xml;charset=utf-8, followed by the XML.

function createXML() {
    var XML = 'data:text/xml;charset=utf-8,<MainNode>';
    var elements = document.getElementsByTagName('input'),i;
    for (i in elements) {
        if (elements[i].checked == true) {
            XML += elements[i].value;
        }
    }
    XML += '</MainNode>';
    window.open(XML);
}

So the url looks like data:text/xml;charset=utf-8,<MainNode><SubNode>...</SubNode>...</MainNode>

Unfortunately this doesn't work for me on Chromium(Chrome) and on Firefox. It just displays the XML instead of showing a save dialog. But I think that's because of my settings and at least you can save it as a XML-file manually.

Baris Akar
  • 4,895
  • 1
  • 26
  • 54
0

I haven't tried this but it should work.

  1. After getting form data, system will call page A.
  2. page A will have javascript that gets query strings and builds the page accordingly.
  3. After finishing page build, user can save current page with following statement in javascript

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

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • 1
    I think force-save works on IE only. See this question: http://stackoverflow.com/questions/833015/does-execcommand-saveas-work-in-firefox – George Jul 29 '11 at 04:46