欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

利用javascript判断文件是否存在_javascript技巧

程序员文章站 2022-04-30 16:22:46
...
判断客户端文件时,可以用
复制代码 代码如下:

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);

判断服务器端(网络文件)时,可以用
复制代码 代码如下:

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",yourFileURL,false);
xmlhttp.send();
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)s+=" exists."; //url存在
else if(xmlhttp.status==404)s+=" doesn't exist."; //url不存在
else s+="";//其他状态
}
alert(s);

可以把contentEditable设置成false限制用户只能选择文件,而不能随便输入.