js 判断文件是否存在

来源:互联网 发布:淘宝网1 编辑:程序博客网 时间:2024/06/05 04:13

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

复制代码
var fso,s=filespec; // filespec="C:/path/myfile.txt"fso=new ActiveXObject("Scripting.FileSystemObject");if(fso.FileExists(filespec))s+=" 文件存在.";elses+=" 文件不存在.";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);

复制代码

checkFileExist:function(fileurl){        var xmlhttp;        //判断浏览器是否支持ActiveX控件        if(window.ActiveXObject){            //支持-通过ActiveXObject的一个新实例来创建XMLHttpRequest对象            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");        }        else if(window.XMLHttpRequest){            //不支持            xmlhttp = new XMLHttpRequest()        }        //var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");        xmlhttp.open("GET",fileurl,false);        xmlhttp.send();        if(xmlhttp.readyState==4){             if(xmlhttp.status==200){                return true;            }            else if(xmlhttp.status==404){                return false; //url不存在             }            else{                return false; //其他状态             }        }     }

0 0
原创粉丝点击