一. 读取本地文件夹以及子文件
const fileBtn = document.querySelector('#file')
fileBtn.onclick = async function () {
try {
const handler = await showDirectoryPicker()
const files = await handlerPicker(handler)
} catch (e) {
console.log('取消')
console.error(new Error(e))
}
}
async function handlerPicker(handler) {
if (handler.kind === 'file') return handler
handler.files = []
const iter = handler.entries()
for await (const item of iter) {
handler.files.push(await handlerPicker(item[1]))
}
return handler
}
二. 读取文件的内容
async function readyFile(file) {
const _file = await file.getFile()
const reader = new FileReader()
reader.readAsText(_file, 'utf-8')
reader.onload = e => {
console.log(e);
console.log(e.target.result)
}
}