1

When Using this Input:

<input type="file" id="file" (change)="useFiles($event)" webkitdirectory directory multiple/>

I get an Object array with all the files inside the folder.

With a fileReader i can read the content and the names of the files:

  public readFolder(files: any[]) {
    this.fileCache = [];
    this.readFile(0, files);
    return this.folderReader$.asObservable();
  }

  private readFile(index, files) {
    const reader = new FileReader();
    if (index >= files.length) {
      this.folderReader$.next(this.fileCache);
      return;
    }
    const file = files[index];
    const filename = file.name;
    reader.onload = (e: any) => {
      this.fileCache.push({
        name: filename,
        content: e.target.result});
      this.readFile(index + 1, files);
    };
    reader.readAsText(file);
  }
}

Now, Is there any possibility to get the name of the Folder using this FileReader? what would be an easy way to get the name of the folder from input?

I am NOT searching on how to get the Path of the files but how to get the name of the folder i am submitting for input.

J.Doe
  • 586
  • 2
  • 8
  • 30
  • Possible duplicate of [How to get path directory from FileReader()?](https://stackoverflow.com/questions/53759070/how-to-get-path-directory-from-filereader) – Liam Oct 01 '19 at 12:48
  • Not path direcotry but the name of the folder i am giving for input – J.Doe Oct 01 '19 at 12:49
  • @Liam when clicking on the input i choose a folder, isn't it possible to get it's name? – J.Doe Oct 01 '19 at 12:50
  • It seems strange to me that I can't get the Name of the Object i'm choosing for input. This has nothing to do with the path has it? – J.Doe Oct 01 '19 at 12:53
  • It seems this is locked down for security reasons. Typically browsers can't interact with the file system of the client. Otherwise virus JS would be a thing. Couple of people here seem to have [potentially working solutions](https://stackoverflow.com/questions/24245105/how-to-get-the-filename-from-the-javascript-filereader) – Liam Oct 01 '19 at 12:58
  • I now found out theres a solution for my question: https://stackoverflow.com/questions/24718769/html5-javascript-how-to-get-the-selected-folder-name is this also not secure? – J.Doe Oct 01 '19 at 13:01
  • 1
    Possible duplicate of [html5/Javascript - How to get the Selected folder name?](https://stackoverflow.com/questions/24718769/html5-javascript-how-to-get-the-selected-folder-name) – J.Doe Oct 01 '19 at 13:01

1 Answers1

1

If you get the whole filePath you can use Node.js path API with basename function:

var path = require('path');
path.basename(path.dirname(filename));
zerocewl
  • 11,401
  • 6
  • 27
  • 53