当前位置: 首页 > news >正文

js中的FileReader对象

一个简单的例子来理解:
读取用户上传的 Excel 文件(.xlsx),解析内容,并以表格形式在页面上显示出来。

它依赖一个外部库:XLSX.js(即 SheetJS),用于解析 Excel 数据。

代码

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><h2>上传Excel文件</h2><input type="file" name="" id="excelFile" accept=".xlsx, .xls"><div id="output"></div><script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script><script src="index.js"></script>
</body>
</html>

index.js

// 读取excel并提取列内容
const UploadFie = document.querySelector("#excelFile")UploadFie.addEventListener("change", handleFile)function handleFile(event) {const file = event.target.files[0]// console.log(event.target.files[0])// const reader = new FileReader()reader.onload = function(e) {const data =  new Uint8Array(e.target.result)const workbook = XLSX.read(data, {type: "array"})const firstSheetName = workbook.SheetNames[0]const worksheet = workbook.Sheets[firstSheetName]// 转为 json 格式const jsonData = XLSX.utils.sheet_to_json(worksheet)// 渲染结果renderTable(jsonData)}// reader.readAsArrayBuffer(file)
}function renderTable(data) {const output = document.querySelector("#output")output.innerHTML = ""if (data.length === 0) {output.textContent = "没有数据"return}const table = document.createElement("table")table.style.border = "1"const thead = table.insertRow()Object.keys(data[0]).forEach(key => {const th = document.createElement("th")th.textContent = keythead.appendChild(th)})data.forEach(row => {const tr = table.insertRow()Object.values(row).forEach(value => {const td = tr.insertCell()td.textContent = value})})output.appendChild(table)
}

带注释js代码

// 获取页面上 ID 为 "excelFile" 的 <input type="file"> 元素,用于文件上传
const UploadFie = document.querySelector("#excelFile");// 给文件上传输入框添加事件监听器,监听 "change" 事件(用户选中文件时触发)
UploadFie.addEventListener("change", handleFile);// 文件选择后的处理函数
function handleFile(event) {// 获取用户选择的第一个文件对象(如果选择多个,只取第一个)const file = event.target.files[0];// 创建一个 FileReader 实例,用于读取文件内容const reader = new FileReader();// 当文件读取完成后触发 onload 回调函数reader.onload = function(e) {// 将读取到的 ArrayBuffer 包装成 Uint8Array 类型的字节数组const data = new Uint8Array(e.target.result);// 使用 SheetJS (XLSX) 读取 Excel 文件内容,指定类型为 "array"const workbook = XLSX.read(data, { type: "array" });// 获取 Excel 的第一个工作表名称const firstSheetName = workbook.SheetNames[0];// 获取第一个工作表对象const worksheet = workbook.Sheets[firstSheetName];// 将工作表数据转换为 JSON 格式数组,每一行为一个对象const jsonData = XLSX.utils.sheet_to_json(worksheet);// 将数据渲染为 HTML 表格并显示在页面上renderTable(jsonData);};// 将文件内容以 ArrayBuffer(二进制缓冲区)方式读取(适用于 Excel)reader.readAsArrayBuffer(file);
}// 渲染 JSON 数据为 HTML 表格,并插入到页面中
function renderTable(data) {// 获取用于显示表格的容器(一般是一个 <div id="output">)const output = document.querySelector("#output");// 清空上一次渲染的内容output.innerHTML = "";// 如果数据为空,显示提示文字并结束函数if (data.length === 0) {output.textContent = "没有数据";return;}// 创建一个新的 HTML 表格元素const table = document.createElement("table");table.style.border = "1"; // 设置表格边框(简易做法)// 创建表头(thead),使用数据的键(字段名)作为列名const thead = table.insertRow(); // 插入第一行作为表头Object.keys(data[0]).forEach(key => {const th = document.createElement("th"); // 创建表头单元格th.textContent = key; // 设置列名(字段名)thead.appendChild(th); // 将表头单元格添加到表头行中});// 遍历数据数组,为每一行数据创建表格行data.forEach(row => {const tr = table.insertRow(); // 创建新行Object.values(row).forEach(value => {const td = tr.insertCell(); // 创建单元格td.textContent = value;     // 填入单元格的值});});// 将完整表格添加到页面上的容器中output.appendChild(table);
}

结构总览 & 难理解的点

代码分为三大部分:

  1. 监听文件上传:UploadFie.addEventListener(...)
  2. 处理上传的文件内容并解析 Excel:handleFile 函数
  3. 将数据渲染成 HTML 表格:renderTable 函数

const data = new Uint8Array(e.target.result);

解释:
上面代码中:reader.readAsArrayBuffer(file); 我们通过 FileReaderreadAsArrayBuffer() 方法来读取文件内容,读取完成后触发 reader.onload,其事件对象 e 中包含的 e.target.result 是一个 ArrayBuffer

ArrayBuffer 是一种原始的、通用的二进制数据容器,不能直接读取内容,需要借助视图(如 Uint8Array, DataView, Float32Array 等)进行解析。


Uint8Array()

  • Uint8Array 是 JavaScript 中的一种类型化数组(TypedArray)
  • 它提供了对 ArrayBuffer 的结构化、字节级别的访问

const workbook = XLSX.read(data, { type: "array" });

  • XLSX.read() 要求的是字节数组
  • SheetJSXLSX.read() 方法如果传入 {type: "array"},它期望的输入是一个 Uint8Array,因此我们必须将原始的 ArrayBuffer 包装为 Uint8Array,否则 XLSX.read() 无法解析

🔬 ArrayBuffer VS Uint8Array

  • ArrayBuffer 就像一块原始磁盘空间,你知道有数据,但无法读取。
  • Uint8Array 就像一个读卡器,它能以 8 位为单位读出这些数据。

http://www.dtcms.com/a/266998.html

相关文章:

  • ESP32CAM通过ESPHome接入HomeAssistant(含无线刷固件等)
  • Python-GUI-wxPython-控件
  • 语音大模型速览(一)F5-TTS
  • 《汇编语言:基于X86处理器》第6章 复习题和练习,编程练习
  • Selenium 安装使用教程
  • Python 量化交易安装使用教程
  • 深度学习3(向量化编程+ python中实现逻辑回归)
  • 遗传算法的原理与实现示例
  • XION:玩转您的第一个智能合约
  • Seq2seq+Attention 机器翻译
  • Go中使用国家新闻出版署实名认证
  • [C++] # 深入理解C++继承:从原理到实现
  • 通过网页调用身份证阅读器http websocket方法-华视电子————仙盟创梦IDE
  • 结构型智能科技的关键可行性——信息型智能向结构型智能的转换(提纲)
  • CSS知识复习2
  • 3-1 PID算法改进(积分部分)
  • Softhub软件下载站实战开发(十二):软件管理编辑页面实现
  • 科学的第五范式:人工智能如何重塑发现之疆
  • 力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
  • 【Qt】QxORM无法删除和更改主键值为0的行,否则报错:invalid primary key
  • Docker学习笔记:Docker网络
  • DotNetBrowser 2.27.14 版本发布啦!
  • Python 制作 pyd(Windows 平台的动态链接库)
  • 力扣:70. 爬楼梯
  • [Terence Tao访谈] 无限 | 关注模型 | 矢量场 | 策略性“作弊” | Lean
  • 【Prometheus 】通过 Pushgateway 上报指标数据
  • 408第三季part1 - 操作系统 - 输入输出管理
  • pyproject.toml 有什么作用呢?
  • Java对象哈希值深度解析
  • vue/微信小程序/h5 实现react的boundary