5

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anandh199g
  • 111
  • 2
  • 3
  • 11

2 Answers2

4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prasath K
  • 4,950
  • 7
  • 23
  • 35
1

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()">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131