是否可以使用FileReader解析csv文件并将其写入WebSql表?
答案 0 :(得分:0)
是的,这应该不是问题:)
只需使用FileReader.readAsText()
来抓取csv文件内容,从那里它应该是轻而易举的
答案 1 :(得分:0)
FileReader.readAsText()方法将为您提供文件中的字符串,然后您可以split()来获取行和csv单元格。查看readAsText()了解更多详细信息,并尝试将以下内容粘贴到交互式示例中:
<script id='csv' type='text/plain'>
apple,1,$1.00
banana,4,$0.20
orange,3,$0.79
</script>
<script>
// Use a Blob to simulate a File
var csv = document.getElementById('csv').textContent.trim();
var file = new Blob([csv]);
var reader = new FileReader();
reader.onload = function(event){
var reader = event.target;
var text = reader.result;
var lines = text.split('\n');
lines.forEach(function(line) {
var parts = line.split(',');
// process the cells in the csv
console.log(parts[0], parts[1], parts[2]);
});
};
reader.readAsText(file);
</script>
答案 2 :(得分:0)
// Use a Blob to simulate a File
let blob = new Blob([
`apple,1,$1.00
banana,4,$0.20
orange,3,$0.79`
])
blob.text().then(text => {
var lines = text.split('\n')
for (let line of lines) {
let parts = line.split(',')
// process the cells in the csv
console.log(parts)
}
})
<script src="https://cdn.rawgit.com/jimmywarting/Screw-FileReader/master/index.js"></script>