I am trying to implement Papa Parse, but I do not want to use the jquery library. Could someone please show me how to parse in normal javascript using a file form my local storage?
Ok, so when I do this I am getting the string value and not the csv values. What am I doing wrong? Also, where do I insert the callback function that I want to use?
function parseMe(url) {
Papa.parse(url, {
complete: function(results) {
console.log(results); // results appear in dev console
}
});
}
parseMe('csv/test.csv');
Close, but file
is not a string, it's a File object obtained from the DOM (docs). To do this, you will need to place a <input type="file">
tag in your page. The user will have to select the file. After they've chosen a file, you can obtain the File object with something like document.getElementById("file").files[0]
-- assuming you've given the input tag an ID of "file" of course.
Also, you can cut out all the cruft in the config object since those are all defaults.
function parseMe(file) {
Papa.parse(file, {
complete: function(results) {
console.log(results); // results appear in dev console
}
});
}
parseMe(document.getElementById("file").files[0]);
Parsing a file is asynchronous so you have to get the results in a callback function which executes later.