我想将我的 csvtojson 函数连接到 html(客户端),但我遇到了一些错误。我应该有一个输入文件的监听器吗?错误之一是不需要定义。但我有一个 package.json 需要它
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<p>Convert CSV to JSON.</p>
<title>Swim API</title>
</head>
<script src='node.js' type='text/javascript'></script>
<body>
<p>Choose a csv to upload</p>
<input name="myFile" type="file" id="getBtn">
<button id="myBtn">Upload</button>
</body>
</html>
脚本.js
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
const csvtojsonV2=require('csvtojson')
function myFunction(){
csvtojsonV2({
noheader:true,
checkType:true,
headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],
})
.fromFile('getBtn')
.then((json) => {
const RESULT = json.reduce((acc, v, i) => {
for (const [key, value] of Object.entries(v)) {
if (!acc[key]) acc[key] = [];
acc[key].push(value);
}
return acc;
}, {});
console.log(RESULT)
})
}
包.json
{
"name": "requirejs",
"license": "MIT",
"volo": {
"url": "https://raw.github.com/requirejs/requirejs/{version}/require.js"
},
"main": "require.js",
"scripts": {
"pretest": "jscs . && jshint require.js"
},
"repository": {
"type": "git",
"url": "https://github.com/requirejs/requirejs.git"
},
"devDependencies": {
"jscs": "^1.12.0",
"jshint": "^2.7.0"
}
}