在JS里,我们可以用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不存在 }








网友评论文明上网理性发言 已有0人参与
发表评论: