我正在尝试在 Vue 中使用 ExcelJS,我需要 FileReader 来读取和解析文件,但我遇到了错误。如何在 VueJS 中使用 FileReader?
输入表格
<input type="file"
id="importProductionSchedule"
name="importProductionSchedule"
@change="checkFile($event)"
ref="importProductionSchedule"
>
检查文件方法
checkFile() {
let reader = new FileReader()
let self = this
reader.onload = (e) => {
let bstr = e.target.result
let wb = XLSX.read(bstr, {type:'binary'})
let wsname = wb.SheetNames[0]
let ws = wb.Sheets[wsname]
let data = XLSX.utils.sheet_to_json(ws, {header:1})
self.form.filedata = data
self.cols = make_cols(ws['!ref'])
}
reader.onerror = (stuff) => {
console.log("error", stuff)
console.log (stuff.getMessage())
}
// reader.readAsArrayBuffer(event)
reader.readAsBinaryString(event.target.files[0])
},
首先,在控制台中记录 event.target.files[0] 会返回文件,但我正在测试 event 和 event.target.files[0] 以确保。
这些是我的错误:
event = Uncaught Error: cannot read as File: {"isTrusted":true}
event.target.files[0] = Uncaught Error: cannot read as File: {}