代码 JavaScript 实现访问本地文件夹

2024-11-18 23:12:47 +0800 CST views 1278

JavaScript 实现访问本地文件夹

首先分享一个 GitHub 的小知识:在 GitHub 域名后加上 1s,即可打开一个 VSCode 窗口来阅读代码。

接下来,介绍如何用 JavaScript 实现访问本地文件夹的功能。考虑到用户隐私,这个功能在之前是不可行的,但新的 API 使之成为可能。下面是一个简单的功能实现示例。

示例代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>读取本地文件内容</title>
</head>
<body>
  <button id="btn">选择文件夹</button>
</body>
</html>

<script>
  // https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker
  const btnDom = document.getElementById("btn");
  btnDom.onclick = async () => {
    try {
      const directory = await window.showDirectoryPicker();
      const root = await processDirectory(directory);
      await renderAllFile(root);
    } catch (error) {
      console.error(error);
    }
  };

  const fileKind = 'file';
  
  const processDirectory = async (directory) => {
    if (directory.kind === fileKind) {
      return directory;
    }
    directory.children = [];
    const iter = directory.entries();
    for await (const item of iter) {
      directory.children.push(await processDirectory(item[1]));
    }
    return directory;
  };

  const renderAllFile = async (root) => {
    const rootType = Array.isArray(root);
    const rootFiles = rootType ? root : [root];
    for (let i = 0; i < rootFiles.length; i++) {
      const rootItem = rootFiles[i];
      if (rootItem.kind === fileKind) {
        await readerFile(rootItem);
      }
      if ((rootItem?.children || []).length) {
        await renderAllFile(rootItem.children);
      }
    }
  };

  const readerFile = async (fileHandle) => {
    const file = await fileHandle.getFile();
    const render = new FileReader();
    render.onload = (e) => {
      const fileText = e.target.result;
      console.log('读取的文件名', file.name);
      console.log('读取的文件内容', fileText);
    };
    render.readAsText(file, 'utf-8');
  };
</script>

说明

  1. 该代码使用 showDirectoryPicker API 获取用户选择的文件夹。
  2. processDirectory 函数递归处理目录,收集文件信息。
  3. renderAllFile 函数读取所有文件并打印文件名和内容。
复制全文 生成海报 编程 Web开发 JavaScript 文件处理

推荐文章

Rust async/await 异步运行时
2024-11-18 19:04:17 +0800 CST
Golang 中你应该知道的 noCopy 策略
2024-11-19 05:40:53 +0800 CST
联系我们
2024-11-19 02:17:12 +0800 CST
PHP 允许跨域的终极解决办法
2024-11-19 08:12:52 +0800 CST
Vue3中怎样处理组件引用?
2024-11-18 23:17:15 +0800 CST
FcDesigner:低代码表单设计平台
2024-11-19 03:50:18 +0800 CST
Vue中的表单处理有哪几种方式?
2024-11-18 01:32:42 +0800 CST
Elasticsearch 条件查询
2024-11-19 06:50:24 +0800 CST
前端代码规范 - 图片相关
2024-11-19 08:34:48 +0800 CST
如何在 Vue 3 中使用 Vuex 4?
2024-11-17 04:57:52 +0800 CST
H5端向App端通信(Uniapp 必会)
2025-02-20 10:32:26 +0800 CST
jQuery中向DOM添加元素的多种方法
2024-11-18 23:19:46 +0800 CST
介绍25个常用的正则表达式
2024-11-18 12:43:00 +0800 CST
curl错误代码表
2024-11-17 09:34:46 +0800 CST
thinkphp swoole websocket 结合的demo
2024-11-18 10:18:17 +0800 CST
PHP解决XSS攻击
2024-11-19 02:17:37 +0800 CST
Plyr.js 播放器介绍
2024-11-18 12:39:35 +0800 CST
PHP中获取某个月份的天数
2024-11-18 11:28:47 +0800 CST
go错误处理
2024-11-18 18:17:38 +0800 CST
程序员茄子在线接单