Javascript 使用javascript创建一个文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5403912/
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
create a text file using javascript
提问by user475685
I am using the following code to create a text file using javascript and it's not working
我正在使用以下代码使用 javascript 创建文本文件,但它不起作用
<html>
<head>
<script language="javascript">
function WriteToFile()
{
var txt = new ActiveXObject("Scripting.FileSystemObject");
var s = txt.CreateTextFile("11.txt", true);
s.WriteLine('Hello');
s.Close();
}
</script>
</head>
<body onLoad="WriteToFile()">
</body>
</html>
采纳答案by Faraona
Try this:
尝试这个:
<SCRIPT LANGUAGE="JavaScript">
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("C:\test.txt", True);
s.writeline("HI");
s.writeline("Bye");
s.writeline("-----------------------------");
s.Close();
}
</SCRIPT>
</head>
<body>
<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form onSubmit="WriteToFile(this)">
Type your first name:
<input type="text" name="FirstName" size="20">
<br>Type your last name:
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit">
</form>
This will work only on IE
这仅适用于 IE
回答by nonozor
That works better with this :
用这个效果更好:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
http://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/5t9b5c0c(v=vs.84).aspx
回答by Joey
From a web page this cannot work since IE restricts the use of that object.
从网页上这不能工作,因为 IE 限制了该对象的使用。
回答by dfortun
You have to specify the folder where you are saving it and it has to exist, in other case it will throw an error.
您必须指定保存它的文件夹并且它必须存在,否则会引发错误。
var s = txt.CreateTextFile("c:\11.txt", true);