Skip to content

Instantly share code, notes, and snippets.

@afreeland
Last active November 11, 2021 11:45
Show Gist options
  • Save afreeland/8184438 to your computer and use it in GitHub Desktop.
Save afreeland/8184438 to your computer and use it in GitHub Desktop.
JavaScript: CSV Get Headers from input stream using ArrayBuffer
function CSVImportGetHeaders()
{
// Get our CSV file from upload
var file = document.getElementById('CSVUpload').files[0]
// Instantiate a new FileReader
var reader = new FileReader();
// Read our file to an ArrayBuffer
reader.readAsArrayBuffer(file);
// Handler for onloadend event. Triggered each time the reading operation is completed (success or failure)
reader.onloadend = function (evt) {
// Get the Array Buffer
var data = evt.target.result;
// Grab our byte length
var byteLength = data.byteLength;
// Convert to conventional array, so we can iterate though it
var ui8a = new Uint8Array(data, 0);
// Used to store each character that makes up CSV header
var headerString = '';
// Iterate through each character in our Array
for (var i = 0; i < byteLength; i++) {
// Get the character for the current iteration
var char = String.fromCharCode(ui8a[i]);
// Check if the char is a new line
if (char.match(/[^\r\n]+/g) !== null) {
// Not a new line so lets append it to our header string and keep processing
headerString += char;
} else {
// We found a new line character, stop processing
break;
}
}
// Split our header string into an array
window.headers = headerString.split(',');
debugger;
// Convert entire ArrayBuffer to string --avoided so not all of ArrayBuffer would have to come into memory
//var arrayToStream = String.fromCharCode.apply(null, new Uint8Array(data));
// Splits on any new line characters and grabs first row, assuming it is headers
//var firstLine = arrayToStream.match(/[^\r\n]+/g)[0];
// Splits on a delimiter
//var delimiterSplit = firstLine.split(',');
};
}
@adalenv
Copy link

adalenv commented Feb 24, 2020

nice job
how can i get second line like headerString ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment