只能用IE 需要加载ActiveX控件
<!DOCTYPE html>
<html><head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="button" value="写入" onClick="writeTxt()">
<input type="button" value="读取" onClick="readTxt()">
<input type="button" value="删除文件" onClick="deleteTxtFile()">
</body>
</html>
<script >
function writeTxt()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile("E:\\test.txt",true);
file = fso.GetFile("E:\\test.txt");
alert(fso.GetFileName(file));
ts = file.OpenAsTextStream(2,true);
ts.WriteLine('123');
ts.WriteLine('呵呵');
ts.Close();
}
function readTxt()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.GetFile("E:\\test.txt");
alert(fso.GetFileName(file));
ts = file.OpenAsTextStream(1,true);
var str = ts.ReadAll();
alert(str);
ts.Close();
}
function deleteTxtFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.GetFile("E:\\test.txt");
alert(fso.GetFileName(file));
fso.DeleteFile(file);
}
</script>