Home  >  Article  >  Web Front-end  >  Javascript determines whether the file exists (client/server side)_javascript skills

Javascript determines whether the file exists (client/server side)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:36:191752browse

Share how to use javascript to determine whether a file exists.

1. When judging client files, you can use

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. When judging the server side (network file), you can use

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

You can520049b3dd3e0744c5b8d59ca0c6e651Set contentEditable to false to restrict users to only select files, not just any Enter.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn