简体   繁体   English

使用浏览器javascript,如何获取用户指定本地目录的文件和子文件夹的内容

[英]Using browser javascript, how to get the contents of the files and subfolders of the user-specified local directory

Please see How to get the contents of the directory from local PC in javascript .请参阅javascript 中的如何从本地 PC 获取目录的内容 There is a link to a jsfiddle that just prints out the names of the files.有一个指向 jsfiddle 的链接,它只打印出文件的名称。 Is it possible to access their content as well?是否也可以访问他们的内容? If yes then how exactly?如果是,那么究竟如何?

May be a Google-Chrome-specific answer... I found https://github.com/GoogleChromeLabs/browser-nativefs .可能是特定于 Google-Chrome 的答案...我找到了 https://github.com/GoogleChromeLabs/browser-nativefs This has a link https://browser-nativefs.glitch.me/ .这有一个链接https://browser-nativefs.glitch.me/ View source and download the html, css and mjs files.查看源代码并下载 html、css 和 mjs 文件。

I modified script.mjs as follows (sharing only the modified part of the code, with comments to indicate the exact modifications):我修改了 script.mjs 如下(仅共享代码的修改部分,并带有注释以指示确切的修改):

  Array.prototype.asyncForEach = async function (fn) {
    for (let i = 0; i < this.length; i++) {
      await fn(this[i], i);
    }
  };

openDirectoryButton.addEventListener('click', async () => {
    try {
      const blobs = await directoryOpen({ recursive: true });
      alert('upload of folder is done')
      //const blobs = await directoryOpen();
      let fileStructure = '';
      await/*added this 'await'*/ blobs.sort((a, b) => {
        a = a.webkitRelativePath + a.name;
        b = b.webkitRelativePath + b.name;
        if (a < b) {
          return -1;
        } else if (a > b) {
          return 1;
        }
        return 0;
      }).asyncForEach/*changed this forEach to asyncForEach*/(async (blob) => {
        // The Native File System API currently reports the `webkitRelativePath`
        // as empty string `''`.
        //added print out of file blob content
        fileStructure += `${blob.webkitRelativePath}${
          blob.webkitRelativePath.endsWith(blob.name) ?
            '' : blob.name}, content: ${await blob.text()}\n`;
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户指定的值获取到javascript文件中 - get user-specified values into javascript file Chrome 扩展程序如何将多个文件保存到用户指定的目录? - How can a Chrome extension save many files to a user-specified directory? 如何在 setTimeout 中测试用户指定的时间 - How to test for user-specified time in setTimeout 如何使用JavaScript获取本地文件夹内容 - How to get local folder contents using JavaScript 如何在javascript中从本地PC获取目录的内容 - How to get the contents of the directory from local PC in javascript 通过用户指定的文件名以Javascript打开文件 - Open file in Javascript from user-specified file name 如何使用Grunt压缩子文件夹/子目录中的所有JavaScript文件? - How to uglify all javascript files in subfolders/subdirectory using Grunt? 如何编写可以生成用户指定数量的 css 方块的脚本 - How to write a script that can produce a user-specified number of css squares 如何在javascript中获取*用户选择的* * local *文件的内容,没有任何帖子,获取,提交等 - How to get the contents of a *user-selected* *local* file in javascript without any posts, gets, submits, etc 如何修改momentjs日历以使周从用户指定的一天开始? - How to modify a momentjs calendar to have weeks start on a user-specified day?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM