收到错误: 类型错误: 无法读取 null 的属性(读取"0")



我想上传一个excel文件,我使用ng-click并在应用程序控制器范围内定义fileUpload($event)函数,并在xlsx的帮助下,我想在JSON中读取文件,但同时运行代码得到这个错误-

<div>
<input id="file-upload" type="file"/>
<input type="button" value="Upload" ng-click ="fileUpload($event)">
</div>
$scope.fileUpload = function(mE) {
const file = mE.target.files[0];
const fileReader = new FileReader();
fileReader.readAsBinaryString(file);
fileReader.onload = (e) => {
var workBook = XLSX.read(fileReader.result, {type: 'binary'});
var sheetNames = workBook.SheetNames;
var ExcelData = XLSX.utils.sheet_to_json(workBook.Sheets[sheetNames[0]]);
console.log(ExcelData);
};
};
TypeError: Cannot read properties of null (reading '0')
at $scope.fileUpload 
at site.js?ver=1:1:47308
at t (site.js?ver=1:1:5384)
at a.$eval (site.js?ver=1:1:4894)
at a.$apply (site.js?ver=1:1:4119)
at HTMLInputElement.<anonymous> (site.js?ver=1:1:5035)
at HTMLInputElement.dispatch (site.js?ver=1:1:200)
at p.handle (site.js?ver=1:1:40)

在你的fileUpload函数中试试

const file  = document.getElementById('file-upload').files[0];
...

相关内容

  • 没有找到相关文章

最新更新