Create Array in javascript reading from local file

2

I need to create an array in javascript of the style var data = [1,2,3,4]; to represent that data on a canvas. The data would be collected from a file on the desktop with an input of the file type.

The content of the file would be:

1
2
3
4

How can I do it? Thanks in advance.

I have this code in which I read the contents of the file, but I do not parse it to the data array.

 
	var datos = [1,1,10,1,1,1,1,1];
	
	function processFiles(files) {
					
	var file = files[0];
	var reader = new FileReader();
	
	reader.onload = function (e) {
	var output = document.getElementById("fileOutput"); 
	output.textContent = e.target.result;
	};
	
	reader.readAsText(file);
	
	var ctx = document.getElementById("grafico").getContext("2d");
	
	var lineChartData = {
	labels: ["0ms", "50ms", "100ms", "150ms", "200ms", "250ms", "300ms", "350ms"],
	datasets: [
			{
			label: ' miliVoltios',
			fillColor: "rgba(20,20,220,0.2)",
			strokeColor: 'rgba(200,2,2,1)',
			pointColor: "#1e45d7",
			pointStrokeColor: "#fff",
			pointHighlightFill: "#fff",
			pointHighlightStroke: "rgba(220,220,220,1)",
			data: datos,
			borderWidth: 4
			},
		]
	}
	var options = {
		scales: {
			yAxes: [{
					ticks: {
						beginAtZero: true
					}
				}],
		}
	};

	window.myPie = new Chart(ctx).Line(lineChartData, {responsive: true}, {options: options});

	}

 
<input id="fileInput" type="file" size="50" onchange="processFiles(this.files)">
	<div id="fileOutput"></div>
    
asked by Alex 09.05.2017 в 13:53
source

1 answer

1

You almost had it already.

At e.target.result you could do split() indicating the separator and you already do array

var datos = [1,1,10,1,1,1,1,1];
	
	function processFiles(files) {
					
	var file = files[0];
	var reader = new FileReader();
	
	reader.onload = function (e) {
	var output = document.getElementById("fileOutput"); 
	output.textContent = e.target.result;
  
  var array = e.target.result.split("\r\n");
  console.log(array);
	};
	
	reader.readAsText(file);
	
	

	
	}
<input id="fileInput" type="file" size="50" onchange="processFiles(this.files)">
	<div id="fileOutput"></div>
    
answered by 09.05.2017 / 14:30
source