当前位置: 首页 > Web前端 > JavaScript

js判断服务器资源是否存在

时间:2023-03-27 11:52:43 JavaScript

不需要下载文件,可以判断文件是否存在/***向指定路径发送下载请求*@param{String}url请求路径*/函数downLoadByUrl(url){varxhr=newXMLHttpRequest();//GET请求,请求路径url,async(是否异步)xhr.open('GET',url,true);//设置响应类型为blobxhr.responseType='blob';xhr.onreadystatechange=function(){console.log("Ready:1",xhr.readyState)console.log("Ready: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();}