關於js中使用ActiveXObject來寫入或讀取檔案的一些知識
阿新 • • 發佈:2019-02-09
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>測試</title>
</head>
<body>
<!-- 寫入資料測試 -->
<div>
<input type="text" id="text">
<input id="1" type="button" value="寫入測試檔案內容" onclick="test(id)">
</div>
<!-- 讀取資料測試 -->
<div>
<input id="2" type="button" value="讀取測試檔案內容" onclick="test(id)">
</div>
<div id="aa"></div>
<script language="javascript">
function test(id){
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(id== 1){
var text = document.getElementById('text').value;
if(text){
var f = fso.createtextfile("D:\\test.txt",true);
f.writeLine(text);
f.close();
alert("寫入資料成功...");
}else{
alert("輸入內容不能為空...");
}
}else{
var ts, s ;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("D:\\test.txt", ForReading);
s = ts.ReadLine();
document.getElementById("aa").innerHTML=s;
}
}
</script>
</body>
</html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>測試</title>
</head>
<body>
<!-- 寫入資料測試 -->
<div>
<input type="text" id="text">
<input id="1" type="button" value="寫入測試檔案內容" onclick="test(id)">
</div>
<!-- 讀取資料測試 -->
<div>
<input id="2" type="button" value="讀取測試檔案內容" onclick="test(id)">
</div>
<div id="aa"></div>
<script language="javascript">
function test(id){
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(id== 1){
var text = document.getElementById('text').value;
if(text){
var f = fso.createtextfile("D:\\test.txt",true);
f.writeLine(text);
f.close();
alert("寫入資料成功...");
}else{
alert("輸入內容不能為空...");
}
}else{
var ts, s ;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("D:\\test.txt", ForReading);
s = ts.ReadLine();
document.getElementById("aa").innerHTML=s;
}
}
</script>
</body>
</html>