[SOLVED] Download list of files from server?

Hello,

I only have skin-deep knowledge of JavaScript.

Before bothering with a server-side scripting language, I was wondering if JavaScript could download the list of files located in a directory on the server, and use them to fill a listbox in the browser:

That way, I would just have to enable directory listing in that directory on the server, and only use Javascript.

Thank you.

To list files on the server you will need server-side scripting, not client-side.

What about downloading the web page, parse it with a regex to build up the list of files, and fill this into a listbox ?

Javascript cannot download a web page, eg. something like:

var mypage = download("http://www.acme.com/mydir/");

?

JavaScript is a client-side language (aside from things like Node.js). It has very limited abilities when it comes to things on a server.

V/r,

^ _ ^

I don’t mind using beefed up JavaScript-based tools like Node.js, if that lets me avoid server-side scripting.

What about using XMLHttpRequest() ? I just tried it, and it does work to retrieve the contents of the page.

The next step would be to use a regex to extract the filenames from the first column.

<html>
<body>
	<script type="text/javascript">
    function loadXMLDoc(theURL)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", theURL, false);
        xmlhttp.send();
    }

	var xmlhttp=false;
	//loadXMLDoc('http://192.168.0.12:8080/list.txt');
	loadXMLDoc('http://192.168.0.12:8080/testdir/');
	if(xmlhttp==false)
		{ /* set timeout or alert() */ }
	else
		{ /* assign `xmlhttp.responseText` to some var */ }
		var contents = xmlhttp.responseText;
		alert(contents);
	</script>
		
</body>
</html>

I don’t understand. Why does the web page list all files in that directory? It sounds like a security hole to me.

Because I told it to by disabling directory listing. As shown (192.168.0.12), it’s a test server on the same computer.

So it looks like JavaScript can query a web page.

Javascript can get a webpage, yes, but it cannot search a file structure on a server. If you’ve manually created a file that contains the directory structure, sure, but that’s not Javascript getting the structure from the server. It’s getting the structure from a file that you manually created. Not the same thing.

And Node.js is a server-side Javascript that doesn’t run client-side. It’s a server-side solution.

V/r,

^ _ ^

1 Like

Me, too.

V/r,

^ _ ^

1 Like

Thanks for the infos.

I’ll see if I can get JavaScript to parse that page and extract the filenames.

I think in most cases it is not desirable to expose folder contents. But I have seen examples where it was desired. eg. WordPress plugin SVN branches.

I would advise to not do this in a higher level htaccess file, but it could work in a folder specific htaccess file for a folder that had no “index” file.

Options +Indexes 

Then JavaScript could parse the DOM to get the URLs from it.

Just be extremely careful about what is getting exposed.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.