Hi,
How to check whether a folder exists or not using Javascript?
Thanks, Karthick
Hi,
How to check whether a folder exists or not using Javascript?
Thanks, Karthick
If this is Javascript running within a web browser, you can't. For (extremely valid) security reasons, Javascript in a web page does not have access to the client filesystem.
If for some reason you wanted to check from a web page whether a folder existed on the server, you could make an Ajax call from Javascript to a server method (e.g. Java, PHP, ASP.NET, etc.) that could (depending on permissions) have access to the server filesystem.
If you are using Javascript as a Windows scripting environment, Microsoft provides a FileSystemObject that includes a FolderExists() method.
If you mean a folder on your server, you can make an ajax request to it and check the status code:
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
if (req.readyState < 4) return;
if (req.status === 200) alert("The folder exists!");
else alert("The folder does not exist or can not be accessed!");
}
req.open("GET", "/path/to/folder", true);
req.send("");