Read csv file automatically
This commit is contained in:
parent
4c0962359d
commit
c255e1ce1c
2 changed files with 11 additions and 29 deletions
|
@ -27,7 +27,7 @@
|
||||||
<table id="table_bla"></table>
|
<table id="table_bla"></table>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
handleFile("world_data_v1.csv")
|
handleFile()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
38
main.js
38
main.js
|
@ -1,36 +1,24 @@
|
||||||
// Source: https://blog.mounirmesselmeni.de/2012/11/20/reading-csv-file-with-javascript-and-html5-file-api/
|
// Source: https://blog.mounirmesselmeni.de/2012/11/20/reading-csv-file-with-javascript-and-html5-file-api/
|
||||||
// Source: https://www.js-tutorials.com/javascript-tutorial/reading-csv-file-using-javascript-html5/
|
// Source: https://www.js-tutorials.com/javascript-tutorial/reading-csv-file-using-javascript-html5/
|
||||||
|
// Source: https://stackoverflow.com/questions/13329853/reading-server-file-with-javascript
|
||||||
|
|
||||||
function handleFile(path) {
|
function handleFile() {
|
||||||
// Check for the various File API support.
|
// Check for the various File API support.
|
||||||
if (window.FileReader) {
|
if (window.FileReader) {
|
||||||
// FileReader are supported.
|
// FileReader are supported.
|
||||||
|
var xmlhttp = new XMLHttpRequest();
|
||||||
getAsText(file);
|
xmlhttp.onreadystatechange = function () {
|
||||||
|
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
|
||||||
|
processData(xmlhttp.responseText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xmlhttp.open("GET", "world_data_v1.csv", true);
|
||||||
|
xmlhttp.send();
|
||||||
} else {
|
} else {
|
||||||
alert('FileReader are not supported in this browser.');
|
alert('FileReader are not supported in this browser.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAsText(fileToRead) {
|
|
||||||
var reader = new FileReader();
|
|
||||||
|
|
||||||
// Read file into memory as UTF-8
|
|
||||||
|
|
||||||
// TODO: fileToRead is the path to the file -> create a file handle
|
|
||||||
|
|
||||||
reader.readAsText(fileToRead);
|
|
||||||
|
|
||||||
// Handle errors load
|
|
||||||
reader.onload = loadHandler;
|
|
||||||
reader.onerror = errorHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadHandler(event) {
|
|
||||||
var csv = event.target.result;
|
|
||||||
processData(csv);
|
|
||||||
}
|
|
||||||
|
|
||||||
function processData(csv) {
|
function processData(csv) {
|
||||||
var table = "<table class='table'>";
|
var table = "<table class='table'>";
|
||||||
var allTextLines = csv.split(/\r\n|\n/);
|
var allTextLines = csv.split(/\r\n|\n/);
|
||||||
|
@ -53,9 +41,3 @@ function processData(csv) {
|
||||||
|
|
||||||
document.getElementById('table_bla').innerHTML = table;
|
document.getElementById('table_bla').innerHTML = table;
|
||||||
}
|
}
|
||||||
|
|
||||||
function errorHandler(evt) {
|
|
||||||
if (evt.target.error.name == "NotReadableError") {
|
|
||||||
alert("Canno't read file !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue