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

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

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 并发执行异步操作
2024-11-18 13:32:18 +0800 CST
阿里云免sdk发送短信代码
2025-01-01 12:22:14 +0800 CST
动态渐变背景
2024-11-19 01:49:50 +0800 CST
百度开源压测工具 dperf
2024-11-18 16:50:58 +0800 CST
php获取当前域名
2024-11-18 00:12:48 +0800 CST
Go 开发中的热加载指南
2024-11-18 23:01:27 +0800 CST
Dropzone.js实现文件拖放上传功能
2024-11-18 18:28:02 +0800 CST
一些实用的前端开发工具网站
2024-11-18 14:30:55 +0800 CST
使用Python实现邮件自动化
2024-11-18 20:18:14 +0800 CST
企业官网案例-芊诺网络科技官网
2024-11-18 11:30:20 +0800 CST
JS 箭头函数
2024-11-17 19:09:58 +0800 CST
imap_open绕过exec禁用的脚本
2024-11-17 05:01:58 +0800 CST
SQL常用优化的技巧
2024-11-18 15:56:06 +0800 CST
MySQL 主从同步一致性详解
2024-11-19 02:49:19 +0800 CST
Vue3中的v-for指令有什么新特性?
2024-11-18 12:34:09 +0800 CST
MySQL死锁 - 更新插入导致死锁
2024-11-19 05:53:50 +0800 CST
ElasticSearch集群搭建指南
2024-11-19 02:31:21 +0800 CST
使用 Go Embed
2024-11-19 02:54:20 +0800 CST
程序员茄子在线接单