zl程序教程

您现在的位置是:首页 >  前端

当前栏目

利用javascript判断文件是否存在

JavaScript文件 利用 判断 是否 存在
2023-06-13 09:15:15 时间
判断客户端文件时,可以用
复制代码代码如下:

varfso,s=filespec;  //filespec="C:/path/myfile.txt"
fso=newActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+="exists.";
else
s+="doesn"texist.";
alert(s);

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

varxmlhttp=newActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",yourFileURL,false);
xmlhttp.send();
if(xmlhttp.readyState==4){  
if(xmlhttp.status==200)s+="exists.";//url存在  
elseif(xmlhttp.status==404)s+="doesn"texist.";//url不存在  
elses+="";//其他状态  
}
alert(s);

可以<inputstyle="width:100%"type="file"name=""id=""  contentEditable="false">把contentEditable设置成false限制用户只能选择文件,而不能随便输入.