利用javascript判断文件是否存在

来源:互联网 发布:西电网络信息安全学院 编辑:程序博客网 时间:2024/06/05 14:54

参考博客:http://www.jb51.net/article/45063.htm


1.判断本地路径的文件是否存在

var fso,s=filespec;   // filespec="C:/path/myfile.txt"fso=new ActiveXObject("Scripting.FileSystemObject");if(fso.FileExists(filespec))    s+=" exists.";else    s+=" doesn't exist.";alert(s);

2.判断网络上文件是否存在

var xmlhttp;    if(window.XMLHttpRequest)    {        xmlhttp = new XMLHttpRequest();//其他浏览器    }    else if (window.ActiveXObject) {        try {            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//旧版IE        }        catch (e) { }        try {            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//新版IE        }        catch (e) { }        if (!xmlhttp) {            window.alert("不能创建XMLHttpRequest对象");        }    }    yourFileURL="https://winycg.github.io/"+textSearch.value+".html"  xmlhttp.open("GET",yourFileURL,false);  xmlhttp.send();  if(xmlhttp.readyState==4){         if(xmlhttp.status==200)           window.location = yourFileURL; //url存在         else         alert("该视频名不存在"); //url不存在     }   





原创粉丝点击