利用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不存在     
}   

  

原文地址:https://www.cnblogs.com/remember-forget/p/9989787.html