Home > front end >  turning filenames of a local directory into a JS array
turning filenames of a local directory into a JS array

Time:07-15

I'm on a local HTML doc and aim to access a folder and scan through the folder adding each file name into a Javascript array

For example, I have a folder called Videos with

VideoA.mp4 VideoB.mp4

How would I then get a Javascript Array with [0] = VideoA.mp4 and [1] = VideoB.mp4 ?

Thanks in advance, please ask any questions If I didn't make myself clear!

CodePudding user response:

Note that this does automatically recurse down the tree of nested folders, I'm looking into removing that.

Also this might load all files into RAM, or might load the metadata of them, I'm not sure.

const videosInput = document.getElementById("videos-input");

videosInput.addEventListener("change", (e) => {
  const files = videosInput.files;
  const fileNames = [...files].filter((file) => file.type === "video/mp4").map((file) => file.name);
  console.log(fileNames, fileNames.length);
  
  // do whatever with `fileNames`
});
<input type="file" id="videos-input" webkitdirectory directory multiple />

  • Related