js 读写文件

读写文件:

var f = fso.CreateTextFile("c:\pexam\"+name+".txt", true);
f.write(arr);
f.Close();
var ForReading=1; 
var fso=new ActiveXObject("Scripting.FileSystemObject"); 
var f=fso.OpenTextFile(src,ForReading,true); 
return(f.ReadAll()); 

判断文件是否存在:

if(fso.FileExists(path))
s+=" 文件存在.";
else
s+=" 文件不存在.";

服务器端的判断:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",yourFileURL,false);
xmlhttp.send();
if(xmlhttp.readyState==4){ 
if(xmlhttp.status==200)s+=" 存在."; //url存在 
else if(xmlhttp.status==404)s+=" 不存在."; //url不存在 
else s+="";//其他状态 
} 
alert(s);

OpenTextFile 函数: 参考:  http://zhidao.baidu.com/link?url=Tbxu1N4PUP9x_mKkXRd1__qG1VfAdAPKf34fgHmLjeaM0BejxnP301PoD5d9rUf9euAlxZmZ3zsJdwD34A4XHq  

new ActiveXObject("Scripting.FileSystemObject");  如果使用 对象的时候报错 “automation服务器不能创建对象”,需要设置信任站点 运行activex控件等操作,参考: http://www.cnblogs.com/sirrah/articles/2349099.html

更加详细的语法参考: http://blog.sina.com.cn/s/blog_addbd3fc01016skf.html 

原文地址:https://www.cnblogs.com/lishupeng/p/5633930.html