Skip to content Skip to sidebar Skip to footer

Is It Possible To Read Files From A Directory Using Javascript?

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?

Solution 1:

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.


Solution 2:

No, it's not possible. Would you like someone to read the contents of your directories?

UPDATE: 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).


Solution 3:

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


Solution 4:

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.


Solution 5:

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/


Post a Comment for "Is It Possible To Read Files From A Directory Using Javascript?"