5

我正在尝试使用 JavaScript 创建一个文本文件。我已经尝试了以下代码,但这不起作用。解决办法是什么?

var fso, file;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.CreateTextFile("c:\\Mytest\test.txt");
file.Close();
4

2 回答 2

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.

于 2013-04-02T13:23:02.843 回答
1

确切地说,ActiveX仅适用于 Internet Explorer。你需要定义一个函数并调用它。

<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()">
于 2013-04-02T13:25:58.987 回答