Last active
December 21, 2020 12:31
-
-
Save PechenkiUA/60c7f5e83b4132b97e8f147dfae73bd3 to your computer and use it in GitHub Desktop.
Read_local_fle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('read').addEventListener('click',file=>{ | |
readFile(document.getElementById('file')) | |
}) | |
function readFile(input) { | |
console.log(input) | |
let file = input.files[0]; | |
let reader = new FileReader(); | |
let readerText = new FileReader(); | |
reader.readAsArrayBuffer(file); | |
readerText.readAsText(file); // is text | |
reader.onload = function() { | |
var resultUtf8is1251 = new TextDecoder('windows-1251').decode(reader.result); | |
var json = csvJSON(resultUtf8is1251); | |
document.querySelector('.result').textContent = json | |
console.log(json) | |
}; | |
// readerText.onload = function() { | |
// var result = readerText.result | |
// document.querySelector('.result').textContent = result | |
// }; | |
reader.onerror = function() { | |
console.log(reader.error); | |
}; | |
} | |
function csvJSON(csv){ | |
var deliminer = ";";// deliminer | |
var lines=csv.split("\n"); | |
var result = []; | |
var headers=lines[0].split(deliminer); | |
for(var i=1;i<lines.length;i++){ | |
var obj = {}; | |
var currentline=lines[i].split(deliminer); | |
for(var j=0;j<headers.length;j++){ | |
obj[headers[j]] = currentline[j]; | |
} | |
result.push(obj); | |
} | |
// return result; //JavaScript object | |
return JSON.stringify(result); //JSON | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment