Skip to content

How to upload and parse CSV file from server and from desktop

Mathias Rangel Wulff edited this page Sep 25, 2017 · 6 revisions

How to upload and parse CSV file from server and from desktop?

You can use [AlaSQL][1] library to load and parse CSV data from server or from desktop.

####From Server You can and parse data with CSV() function:

    alasql(['SELECT * FROM CSV("mydata")']).then(function(data) {
        // Use data
    });

####From Desktop If you want to upload data from desktop you can use the following code:

     <script src="alasql.min.js"></script>
     <p>Select CSV file to read:</p>
     <input id="readfile" type="file" onchange="loadFile(event)"/>
     <script>
         function loadFile(event) {
            alasql(['SELECT * FROM FILE(?,{headers:true})',[event]]).then(function(data){
            	// Process data here
            });
         }
     </script>

See this jsfiddle for a live example

For people working with react.js: you need to use the nativeEvent from the event:

    window.alasql('SELECT * FROM FILE(?,{headers:true})', [e.nativeEvent], function (data) {
      console.log(data)
    });

See also How to upload form for txt and xlsx in javascript

Clone this wiki locally