您的位置:首页 > Web前端 > JavaScript

利用javascript判断文件是否存在

2017-12-13 16:56 211 查看
参考博客: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不存在
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: