我使用以下代码使用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>
答案 0 :(得分:3)
试试这个:
<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>
这仅适用于IE
答案 1 :(得分:3)
这样做效果更好:
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
答案 2 :(得分:2)
从网页开始,这不起作用,因为IE限制了该对象的使用。
答案 3 :(得分:0)
您必须指定保存它的文件夹,并且必须存在,否则会引发错误。
var s = txt.CreateTextFile("c:\\11.txt", true);