I am trying to supply a user-selected csv file through input type="file"
and read it using JQuery's $.ajax()
then display it to an empty html table. For some reason, it's unable to see the file that was selected by html's file chooser.
I'm stuck on this part and don't know how to get the path or whether or not I'd have to supply the path of x.csv file. I did some research and found about this.files[0]['mozFullPath']
but seems to be something applicable only when using Mozilla Firefox.
Is there any better or correct approach to what I'm trying to do?
$('#csv_file_input').on('change', function () {
var fileInput = $('#csv_file_input')[0].files[0].name;
console.log(fileInput);
$.ajax({
url: fileInput, //404 not found
dataType: "text",
success: function (data) {
var employee_data = data.split(/\r?\n|\r/);
var table_data = '<table class="table table-bordered table-striped">';
for (var count = 0; count < employee_data.length; count++) {
var cell_data = employee_data[count].split(",");
table_data += '<tr>';
for (var cell_count = 0; cell_count < cell_data.length; cell_count++) {
if (count === 0) {
table_data += '<th>' + cell_data[cell_count] + '</th>';
}
else {
table_data += '<td>' + cell_data[cell_count] + '</td>';
}
}
table_data += '</tr>';
}
table_data += '</table>';
$('#data_preview').html(table_data);
}
});
});
I prefer to achieve this without using other libraries.
I get 404 not found on the url
part of $.ajax()
block which I suspect to be because of selected file's path incorrectly read or path missing.
I'd appreciate any help or suggestion.
Thank you.