Use javascript to determine if a file exists

  • 2020-03-30 01:11:35
  • OfStack

Can be used to determine client files

var fso,s=filespec;   // filespec="C:/path/myfile.txt"
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso.FileExists(filespec))
s+=" exists.";
else
s+=" doesn't exist.";
alert(s);

Can be used to determine the server side (network file)

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",yourFileURL,false);
xmlhttp.send();
if(xmlhttp.readyState==4){   
if(xmlhttp.status==200)s+=" exists."; //Url exists    
else if(xmlhttp.status==404)s+=" doesn't exist."; //Url does not exist & cake;  
else s+="";//Other states & NBSP;  
} 
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: