如何使用 JavaScript 创建文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15765189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:01:58  来源:igfitidea点击:

How to create a text file using JavaScript?

javascriptgoogle-chrometext-filescreationfilesystemobject

提问by anandh199g

I am trying to create a text file using javascript. I have tried the following code ,but this didnt work. please give me a solution . Thank you .

我正在尝试使用 javascript 创建一个文本文件。我试过下面的代码,但这没有用。请给我一个解决方案。谢谢你 。

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

回答by Prasath K

You cannot do it using ActiveXObject as it works only in IE .. Have a look on File System APIs of HTML5which may help you

您不能使用 ActiveXObject 来完成它,因为它仅适用于 IE .. 看看HTML5 的文件系统 API,这可能对您有所帮助

回答by Roberto Osses Elgueta

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

没错,ActiveX 只适用于 IE。你需要定义一个函数并调用它。

<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()">