
在JavaScript中,可以通过ActiveXObject对象判断本地文件路径来判断本地文件是否存在,网络文件通过判断其URL是否存在来判断文件是否存在。
javascript判断文件是否存在的方法:
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不存在
}以上就是javascript怎么判断文件是否存在?的详细内容,更多请关注易知道|edz.cc其它相关文章!














