关于javascript:js判断服务器资源存在不存在

2次阅读

共计 470 个字符,预计需要花费 2 分钟才能阅读完成。

不须要下载文件,就能够判断文件存在不存在

/**
* 向指定门路发送下载申请
* @param{String} url 申请门路
*/
function downLoadByUrl(url){var xhr = new XMLHttpRequest();
        //GET 申请, 申请门路 url,async(是否异步)
        xhr.open('GET', url, true);
        // 设置响应类型为 blob
        xhr.responseType = 'blob';
        xhr.onreadystatechange = function(){console.log("筹备好:1",xhr.readyState)
            console.log("筹备好:2",xhr.status)
            if(xhr.readyState==3 && xhr.status==200){console.log("文件存在");
                // 完结接管流,不再接管 要害操作
                xhr.abort();}
            if(xhr.readyState==3 && xhr.status==404){console.log("文件不存在");
            }
        }
    
        // 发送申请
        xhr.send();}
正文完
 0