5

Possible Duplicate:
Download textarea contents as a file using only Javascript (no server-side)

I have a form which shows some user related information in a textarea. If user want to save the information, he/she will copy the text from textarea then click on [Save] button, a save as dialog appear to allow user to choose an appropriate path then export the selected text to text file

The problem is that i don't know how to display the Save as dialog and write to the selected path as text file at client site (It may use Javascript or Jquery?). So i wonder if someone could give me some hint?

Thank you very much.

Community
  • 1
  • 1
Phu Nguyen
  • 451
  • 3
  • 8
  • 12
  • 1
    I don't think you can do that. – sje397 Dec 16 '10 at 06:42
  • @jleedev: yet the accepted answer was server side. – bob-the-destroyer Dec 16 '10 at 07:43
  • 1
    @tlpd: it's impossible in javascript unless the client has IE on Windows and their security permission allows for the creation of COM objects (which IE forbids by default). You may want to expand your search and tag choices to include flash, java applets, etc. – bob-the-destroyer Dec 16 '10 at 07:47
  • @bob-the-destroyer : because that's not possible in JS :) – Valentin Rocher Dec 16 '10 at 08:05
  • @Valentin Rocher: The original question was "can you do it with javascript alone (non browser specific)". If you wanted, you can wipe a client's hard-drive using just javascript if they have IE, Windows, and they allow it through security permissions. Shadow Wizard mentioned the `execCommand` technique as a workaround for this particular question. Otherwise, yeah, you'll have to bring in something besides javascript to access the client's machine especially across different browsers. But if IE is your only target audience and they trust you, just bring in `ActiveXObject()` – bob-the-destroyer Dec 17 '10 at 05:44

1 Answers1

13

IE only solution:

function SaveContents(element) {
    if (typeof element == "string")
        element = document.getElementById(element);
    if (element) {
        if (document.execCommand) {
            var oWin = window.open("about:blank", "_blank");
            oWin.document.write(element.value);
            oWin.document.close();
            var success = oWin.document.execCommand('SaveAs', true, element.id)
            oWin.close();
            if (!success)
                alert("Sorry, your browser does not support this feature");
        }
    }
}

Required HTML sample:

<textarea id="myText"></textarea><br />
<button type="button" onclick="SaveContents('myText');">Save</button>

This will save the contents of the given textarea into a file with name equal to the ID of the textarea.

As for other browsers, you can read this: Does execCommand SaveAs work in Firefox?

Test case and working example: http://jsfiddle.net/YhdSC/1/ (IE only..)

NOTE: https://support.microsoft.com/en-us/help/281119/internet-explorer-saves-html-content-instead-of-the-active-document

It may not work for filetypes other than txt

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208