Created
May 5, 2015 11:25
-
-
Save cmatskas/1d26cfa2bf76a94b723e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
$(document).ready(function() { | |
// The event listener for the file upload | |
document.getElementById('txtFileUpload').addEventListener('change', upload, false); | |
// Method that checks that the browser supports the HTML5 File API | |
function browserSupportFileUpload() { | |
var isCompatible = false; | |
if (window.File && window.FileReader && window.FileList && window.Blob) { | |
isCompatible = true; | |
} | |
return isCompatible; | |
} | |
// Method that reads and processes the selected file | |
function upload(evt) { | |
if (!browserSupportFileUpload()) { | |
alert('The File APIs are not fully supported in this browser!'); | |
} else { | |
var data = null; | |
var file = evt.target.files[0]; | |
var reader = new FileReader(); | |
reader.readAsText(file); | |
reader.onload = function(event) { | |
var csvData = event.target.result; | |
data = $.csv.toArrays(csvData); | |
if (data && data.length > 0) { | |
alert('Imported -' + data.length + '- rows successfully!'); | |
} else { | |
alert('No data to import!'); | |
} | |
}; | |
reader.onerror = function() { | |
alert('Unable to read ' + file.fileName); | |
}; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment