
function handleFiles(files) {
    // Check for the various File API support.
    if (window.FileReader) {
        // FileReader are supported.
        getAsText(files[0]);
    } else {
        alert('FileReader are not supported in this browser.');
    }
}

function getAsText(fileToRead) {
    var reader = new FileReader();
    // Read file into memory as UTF-8      
    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) {
    var allTextLines = csv.split(/\r\n|\n/);
    var lines = [];
    for (var i = 0; i < allTextLines.length; i++) {
        var data = allTextLines[i].split(';');
        var tarr = [];
        for (var j = 0; j < data.length; j++) {
            tarr.push(data[j]);
        }
        lines.push(tarr);
    }
    
    var myJsonString = JSON.stringify(lines);

    renderTable(myJsonString);
}
function renderTable(txt){
 
// $.parseJSON will parse the txt (JSON) and convert it to an
// JavaScript object. After its call, it gets the employees property
// and sets it to the employees variable
var listings =  $.parseJSON(txt);
var $table = $( "<table><tr>\n\
<th>SKU</th>\n\
<th>Publicación</th>\n\
<th>Título</th>\n\
<th>Categoría Base</th>\n\
<th>Descripción</th>\n\
<th>Precio</th>\n\
<th>Stock</th>\n\
</tr></table>" );



for ( var i = 0; i < listings.length; i++ ) {
    
    var  listing = listings[i];
    console.log(JSON.stringify(listing));

    var $line = $( "<tr></tr>" );
    $line.append( $( "<td></td>" ).html( listing.sku ) );
    $line.append( $( "<td></td>" ).html( listing.title ) );
    $table.append( $line );
    
}

$table.appendTo( document.body );

// if you want to insert this table in a div with id attribute 
// set as "myDiv", you can do this:
$table.appendTo( $( "#myDiv" ) );
    
}

function errorHandler(evt) {
    if (evt.target.error.name == "NotReadableError") {
        alert("Canno't read file !");
    }
}