[英]Getting Error: TypeError: Cannot read properties of null (reading '0')
I want to upload an excel file for that I used ng-click and define the fileUpload($event) function in the app controller scope and with the help of xlsx I want to read the file in JSON but while running the code getting this error-我想上传一个 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)
Try this in your fileUpload function在你的 fileUpload 函数中试试这个
const file = document.getElementById('file-upload').files[0];
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.