Reader onload 函数未向预定义数组添加分割线
Reader onload function not adding split lines to predefined Array
我正在尝试使用 html 中的输入标签加载文件,类型为 'file'
<input type="file">
然后使用此文件,我尝试将每一行拆分,并将结果数组 return 分成一个名为 lines
.
的数组
我尝试移动 console.log(lines),但只有当 console.log 在 .onload 函数中时,我才得到正确的结果。
var input = document.querySelector('input[type="file"]')
input.addEventListener('change', function (e) {
let lines = new Array();
console.log(input.files)
const reader = new FileReader()
reader.onload = function () {
lines = reader.result.split('\n');
}
reader.readAsText(input.files[0]);
console.log(lines)
})
我怎样才能确保 lines
数组中放入了正确的分割线,这样我就可以在我的整体功能的其他部分使用 lines
数组
只需尝试在数组中推送拆分值。它可能适合你。
var input = document.querySelector('input[type="file"]')
let lines = [];
input.addEventListener('change', function (e) {
const reader = new FileReader()
reader.onload = function () {
lines.push(reader.result.split('\n'));
}
reader.readAsText(input.files[0]);
console.log(lines);
})
<input type="file">
我正在尝试使用 html 中的输入标签加载文件,类型为 'file'
<input type="file">
然后使用此文件,我尝试将每一行拆分,并将结果数组 return 分成一个名为 lines
.
我尝试移动 console.log(lines),但只有当 console.log 在 .onload 函数中时,我才得到正确的结果。
var input = document.querySelector('input[type="file"]')
input.addEventListener('change', function (e) {
let lines = new Array();
console.log(input.files)
const reader = new FileReader()
reader.onload = function () {
lines = reader.result.split('\n');
}
reader.readAsText(input.files[0]);
console.log(lines)
})
我怎样才能确保 lines
数组中放入了正确的分割线,这样我就可以在我的整体功能的其他部分使用 lines
数组
只需尝试在数组中推送拆分值。它可能适合你。
var input = document.querySelector('input[type="file"]')
let lines = [];
input.addEventListener('change', function (e) {
const reader = new FileReader()
reader.onload = function () {
lines.push(reader.result.split('\n'));
}
reader.readAsText(input.files[0]);
console.log(lines);
})
<input type="file">