<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ddd.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
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; }
首先权限问题,每次都会提示用户
其次你用ActiveX,只支持IE
这个感觉毫无意义
<html>
<head>
<title>ddd.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
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;
}
//写文件
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');
}
</script>
</head>
<body>
<input type="text" id="in" name="in" />
<input type="button" value="Write!"
onclick="writeFile('c:/12.txt',document.getElementById('in').value);" />
<br>
<br>
<input type="button" value="Read!"
onclick="document.getElementById('show').value=readFile('c:/12.txt');" />
<br>
<textarea id="show" name="show" cols="50" rows="8">
</textarea>
</html>可能需要些设置 如果说提示automation服务器不能创建对象 运行 敲 regsvr32 scrrun.dll 打开文件读写功能 然后设置浏览器安全级别中
打个小广告. 只需要1分钟, 轻松拥有自己的DIY网站 http://www.imaiyi.com
JS中用AJAX请求读取服务端文件是可行的,但是有游览器限制,FF和Safari测过没问题