将文件读为字符串
确保在页面上输入文件:
<input type="file" id="upload">
然后在 JavaScript 中:
document.getElementById('upload').addEventListener('change', readFileAsString)
function readFileAsString() {
var files = this.files;
if (files.length === 0) {
console.log('No file is selected');
return;
}
var reader = new FileReader();
reader.onload = function(event) {
console.log('File content:', event.target.result);
};
reader.readAsText(files[0]);
}