一、html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<button class="save_button" onclick="f_load_files()">载入文件</button>
<input type="file" id="selectFiles" webkitdirectory/>
<body>
</body>
</html>
二、js
files_content = [];
//因为载入文件是异步操作,函数必须采用asyn/await解决
async function f_load_files(){
var selectFiles = document.getElementById("selectFiles").files;
files_content =[];
for(var file of selectFiles){
const readFileAsync = file => new Promise(resolve => {
const reader = new FileReader();
reader.onload = evt => resolve(evt.target.result)
reader.readAsText(file)
})
files_content.push(await readFileAsync(file));
}
alert(files_content);
}