18

Is it possible to list the files in a directory using only javascript? To clarify, I mean list the files on the server, not the files on the clients computer. For instance:

www.domain.com/files/

contains 4 images (.jpg)

Can I make an extra page (www.domain.com/files/list.html) that lists those 4 files using javascript?

3
  • It's possible, but you can't access the user's filesystem in a cross-browser way, and not without the user explicitly agreeing (via a very obtrusive dialig box) even in the browsers that do support it. Best clarify that this is what you're after to make sure the answers you're getting will be helpful
    – Gareth
    May 27, 2010 at 21:24
  • To clarify, I mean list the files on the server, not the files on the clients computer. For instance: www.domain.com/files/ contains 4 images (.jpg) Can I make an extra page (www.domain.com/files/list.html) that lists those 4 files using javascript? May 27, 2010 at 22:07
  • It's 2018, I'm quite sure JS can list files at a given url. Does anyone have a good answer to this question?
    – Fla
    Jan 18, 2018 at 19:17

5 Answers 5

12

No, Javascript doesn't have access to the filesystem. Server side Javascript is a whole different story but I guess you don't mean that.

1
  • 2
    To clarify, I mean list the files on the server, not the files on the clients computer. For instance: www.domain.com/files/ contains 4 images (.jpg) Can I make an extra page (www.domain.com/files/list.html) that lists those 4 files using javascript? May 27, 2010 at 22:07
11

Very late to this party, but my google search for this exact request led me here.

The answer is "not really", but I've found the frankenstein of hacks elsewhere: If +Indexes is (or can be) enabled in the .htaccess for the folder containing the files you want to list, then use XMLHTTPRequest with the folder name as the url (which will return an html page listing the files).

0
1

I don't know if you architecture allows it but ikf you can install and use node.js as its node API mentions, you can interact with the filesystem by requiring the fs module.

This is the environment Node.js relies on:

Node eventually wants to support all POSIX operating systems (including Windows with MinGW) but at the moment it is only being tested on Linux, Macintosh, and Solaris. The build system requires Python 2.4 or better. V8, on which Node is built, supports only IA-32 and ARM processors. V8 is included in the Node distribution. To use TLS, OpenSSL are required. There are no other dependencies.

You can run It side-by-side with another web app. and this will avoid blocking your web application if the interaction with the filesystem takes too long.

0

It is generally not a good idea to access client computer files via javascript for security reasons, however i suspect you can use the File System Object for that. I am not sure about browser-compatibility for that, it should work in IE only probably though.

You need to use server-side languages such as PHP, ASP.Net, JSP, etc

0

JavaScript runs inside a host environment. So if the host provides a facility to list files in this manner, then yes. But in the typical scenario where JavaScript is running in a browser with default configuration, no.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.