4

I want to read a directory and fill a list with the name of those files.

Is it possible to do this tasks using javascript?

4
  • 2
    Depends. Browser-JavaScript: Real file-system: No. Virtual filesystem: Yes. Commented Jul 4, 2012 at 15:36
  • Virtual filesystem is, as I understand it, just another way of using localStorage; it's not possible to use it to do ls something, I suppose. ) Commented Jul 4, 2012 at 15:40
  • I know this is not what you're looking for, but you can create a server-side script and make it read a directory then send the results as JSON. Commented Jul 4, 2012 at 15:40
  • ... if the poster wants to get contents of his directory, not a client's one. ) Commented Jul 4, 2012 at 15:41

6 Answers 6

5

No, for security reasons.

You might be able to do it by invoking ActiveX or Flash and having the user agree to permit file system access from these plugins, but - please don't.

Edit 10 years on: Not only please don't do this, but now outside of using an old device without updates - you can't do this with Flash/ActiveX.

Sign up to request clarification or add additional context in comments.

2 Comments

Flash and ActiveX are no longer supported in most browsers, but it is still possible to read files from directories in browsers that support the File System Access API.
I managed to make it work with this library: browser-fs-access
4

It's 2022, a lot of changes in the world and beyond, and, lo and behold, now we have something called File System Access API:

This API allows interaction with files on a user's local device, or on a user-accessible network file system. Core functionality of this API includes reading files, writing or saving files, and access to directory structure.

It became available in Chrome 86 (released in October 2020). Safari added support in 15.2 version, released in the end of 2021. To the moment of writing this, Firefox doesn't support this feature though (here's the related discussion).

Also, security considerations didn't go anywhere:

This API opens up potential functionality the web has been lacking. Still, security has been of utmost concern when designing the API, and access to file/directory data is disallowed unless the user specifically permits it.


This part is no longer relevant (kudos to @gignu for mentioning that in the comments) and is left here for historical reasons.

I suppose the closest you might get by using webkitdirectory attribute:

HTML

<input type="file" id="file_input" webkitdirectory="" directory="" />
<div id="list_of_files"></div>
...

JS

var $list = $('#list_of_files');
$('#file_input').change(function(event) {
  var listOfFiles = event.target.files;
  for (var i = 0, l = listOfFiles.length; i < l; ++i) {
     $list.append($('<p>'+ listOfFiles[i].name +'</p>'));
  }
});

... as shown here. But it works in Chrome only (and suggested usage of mozdirectory attribute didn't help).

3 Comments

It's not possible? I disagree! Take a look at this library, it does exactly what the OP was asking for: browser-fs-access
This library is a wrapper around (relatively new) API implemented in 2020, and this answer was written in 2012. :) But you're right; updated my answer mentioning this new API.
Cool, thx for updating it!!
2

You can try to use FileReader object, but it poorly supported by browsers.

Comments

1

In google chrome you may prompt the client to select a directory and then use this to list the files contained in the directory and its subdirectories:

<input type="file" webkitdirectory onchange="listContents(this.files)">

listContents would be your implementation.

Comments

0

I don't know if you're doing security research etc etc.. So besides from saying "you shouldn't do it", the actual answer to the question is, you can actually READ files by taking advantage poorly-written JS code, that's why you should code.. defensively.

Then there's this: http://www.html5rocks.com/en/tutorials/file/dndfiles/

Comments

0

Yes depending on the browser you have.

Even though it is not a common practice but you can using certain browsers such as Chrome (using the requestFileSystem supported by webkitRequestFileSystem) or in Internet Explorer using File System Object.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.