js读取txt文件:
function readFile(filename){var fso = new ActiveXObject("Scripting.FileSystemObject")
var f = fso.OpenTextFile(filename,1)
var s = ""
while (!f.AtEndOfStream)
s += f.ReadLine()+"\n"
f.Close()
return s
}
js写txt文件:
function writeFile(filename,filecontent){var fso, f, s
fso = new ActiveXObject("Scripting.FileSystemObject")
f = fso.OpenTextFile(filename,8,true)
f.WriteLine(filecontent)
f.Close()
alert('ok')
}
<!DOCTYPE html><html>
<head>
<title>文件示例</title>
<meta name="name" content="content" charset="utf-8">
</head>
<body>
<input type="file" id="file" />
<input type="button" onclick="readText()" value="File Button">
<div id="tt">
</div>
</body>
</html>
<script charset="utf-8">
window.onload=function () {
if(typeof(FileReader)=="undefined")
{
alert("你的浏览器不支持文件读取")
document.write("")
}else
{
alert("你的浏览器支持文件读取")
}
}
function readText() {
var file=document.getElementById("file").files[0]
var reader=new FileReader()
reader.readAsText(file)
reader.onload=function(data)
{
var tt=document.getElementById("tt")
tt.innerHTML=this.result
}
}
</script>