Upload a CSV in JavaScript

Let's assume that you want to upload a CSV using JavaScript.

Let's define the HTML block:

html
<input type = 'file' id = 'csv' accept = '.csv'></input>

And now let's create the function:

javascript
document.getElementById('csv').addEventListener('change', e => {

    let reader = new FileReader();

    reader.onload = () => {

        let csv = (reader.result).split(/\n|\r/);

        console.log(csv);
        
    };
    
    reader.readAsText(e.target.files[0]);
  
});

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.