×

用ActiveXObject对象判断本地文件路径来判断本地文件是否存在

作者:andy0012020.04.13来源:Web前端之家浏览:9114评论:0
关键词:jsActiveXObject

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

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/jswenjianu834058304950.html

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

发表评论: