js 判断文件是否存在

判断客户端文件时,可以用

var fso,s=filespec; // filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+=" 文件存在.";
else
s+=" 文件不存在.";
alert(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);
posted @ 2012-05-16 11:02  Relict  阅读(31162)  评论(2编辑  收藏  举报