1

I'm trying to save input from a textarea to a .txt file.

Another textarea will be used for the name of the file (example: important - it saves as important.txt in a chosen directory)

What I've got so far:

<html>
    <head>
        <script language="javascript">
            function WriteToFile()
            {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var s = fso.CreateTextFile("C:\inetpub\wwwroot\myfile.txt", true);
            var text=document.getElementById("TextArea1").innerText;
            s.WriteLine(text);
            s.WriteLine('***********************');
            s.Close();
            }
        </script>
    </head>
    <body>
        <form>
            <div>
                <textarea id="TextArea1" height: style="width: 588px; height: 90px" 90px">Write here</textarea><br />
                <input id="Button1" type="button" value="Write" onclick="WriteToFile()"/>
            </div>
        </form>
    </body>
</html>
Mikkel
  • 7,693
  • 3
  • 17
  • 31
aerond
  • 75
  • 1
  • 2
  • 8

1 Answers1

0

Something like this?

<html>
<head>
<script language="javascript">
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var text=document.getElementById("TextArea1").innerText;
var fileName=document.getElementById("TextArea2").innerText;
var s = fso.CreateTextFile("C:\\inetpub\\wwwroot\\"+fileName+".txt", true);
s.WriteLine(text);
s.WriteLine('***********************');
s.Close();
}
</script>

</head>
<body>
<form>
<div>
<textarea id="TextArea1" height: style="width: 588px; height: 90px" 90px">Write here</textarea><br />
<textarea id="TextArea2" height: style="width: 588px; height: 90px"   90px">Type File name</textarea>
<input id="Button1" type="button" value="Write" onclick="WriteToFile()"/>
</div> 
</form>
</body>
</html>

BTW I think that ActiveXObject only works in IE browsers.
Check ActiveXObject in Firefox or Chrome (not IE!)

Community
  • 1
  • 1
Gest0
  • 16
  • 3
  • first of all, thank you for your reply! And yes this is what I meant. I tried your code, if I click the button it doesn't do anything for some reason – aerond Dec 16 '16 at 14:33
  • @aerond I just tested it and it works in my local with my own path. Check that the path is correct and exists in your computer: C:\inetpub\wwwroot\ and check if you have permissionsto write in that folder. You could also check if there is any error in the console on your IE (right click on your page -> inspect element -> console tab -> and then click the write button to see if anything fails) – Gest0 Dec 16 '16 at 15:14