<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
<button>打开文件夹</button>
<div id="content"></div>
<script>
const button = document.querySelector('button');
button.addEventListener('click', async () => {
let handle = await showDirectoryPicker();
await processHandle(handle);
const fileHandle = handle.children[7];
console.log('handle: ', handle);
const file = await fileHandle.getFile();
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
const content = document.querySelector('#content');
content.textContent = reader.result;
}
})
async function processHandle(handle) {
handle.children = []
if(handle.kind === 'file') return
const values = await handle.values();
for await (const item of values) {
handle.children.push(item)
await processHandle(item)
}
}
</script>
</body>
</html>