龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

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

时间:2014-09-23 02:00来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了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.jb51.net
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限制用户只能选择文件,而不能随便输入.

精彩图集

赞助商链接