Javascript determines whether the file exists on the client and server side

  • 2020-03-30 03:58:16
  • OfStack

Share how javascript determines if a file exists.

1, can be used to judge the client file


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, can be used to determine the server side (network files)


var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",yourFileURL,false);
xmlhttp.send();
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)s+=" exists."; //The url is
else if(xmlhttp.status==404)s+=" doesn't exist."; //The url was not found
else s+="";//Other states
} // www.yuju100.com
alert(s);

Can < Input style="width:100%" type="file" name="" id="" contentEditable="false" > Setting contentEditable to false limits the user's ability to select files instead of typing in them.


Related articles: