Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a text file using JavaScript? [duplicate]

I am trying to create a text file using JavaScript. I have tried the following code, but this didn’t work. What is the solution?

var fso, file;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.CreateTextFile("c:\\Mytest\test.txt");
file.Close();
like image 529
anandh199g Avatar asked Apr 02 '13 13:04

anandh199g


2 Answers

You cannot do it using ActiveXObject as it works only in Internet Explorer... Have a look on File System APIs of HTML5 which may help you.

like image 169
Prasath K Avatar answered Sep 22 '22 16:09

Prasath K


Exactly, ActiveX only works in Internet Explorer. And you need define a function and call this.

<script>
    function wtf() {
        set fso = CreateObject("Scripting.FileSystemObject");
        set s = fso.CreateTextFile("C:\test.txt", True);
        s.writeline("HI");
        s.writeline("Bye");
        s.writeline("-----------------------------");
        s.Close();
    }
</script>

<body onload="wtf()">
like image 39
Roberto Osses Elgueta Avatar answered Sep 22 '22 16:09

Roberto Osses Elgueta