首页 > 语言 > JavaScript > 正文

Javascript判断文件是否存在(客户端/服务器端)

2024-05-06 16:08:53
字体:
来源:转载
供稿:网友
这篇文章主要介绍了Javascript判断文件是否存在的方法适用于客户端、服务器端,远程文件,示例代码如下,需要的朋友可以参考下
分享下javascript判断文件是否存在的方法。
1,判断客户端文件时,可以用

var fso,s=filespec; // filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+=" exists.";
else // www.vevb.com
s+=" doesn't exist.";
alert(s);
2,判断服务器端(网络文件)时,可以用

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+="";//其他状态
} // www.yuju100.com
alert(s);
可以<input style="width:100%" type="file" name="" id="" contentEditable="false" >把contentEditable设置成false限制用户只能选择文件,而不能随便输入.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选