Does anyone know of a way to get data from a CSV file, then put it into an array which i can pass via AJAX to my webservice?
Asked
Active
Viewed 2,908 times
2 Answers
1
Replaced my entire answer with a nice jQuery solution:
$(document).ready(function() {
var data = '"REVIEW_DATE","AUTHOR","ISBN","DISCOUNTED_PRICE" "1985/01/21","Douglas Adams",0345391802,5.95 "1990/01/12","Douglas Hofstadter",0465026567,9.95 "1998/07/15","Timothy ""The Parser"" Campbell",0968411304,18.99 "1999/12/03","Richard Friedman",0060630353,5.95 "2001/09/19","Karen Armstrong",0345384563,9.95 "2002/06/23","David Jones",0198504691,9.95 "2002/06/23","Julian Jaynes",0618057072,12.50 "2003/09/30","Scott Adams",0740721909,4.95 "2004/10/04","Benjamin Radcliff",0804818088,4.95 "2004/10/04","Randel Helms",0879755725,4.50';
var array1 = data.split("\t");
var array2 = new Array();
for (var i = 0; i < array1.length; i++) {
array2.push(array1[i].split(","));
for (var j = 0; j < array2[i].length; j++) {
$('ul').append('<li>'+array2[i][j]+'</li>');
}
$('ul').append('<li><hr></li>');
}
});
Only difference is you will have to do the following to retrieve the CSV file...
$.ajax({
url: "file.csv",
success: function(data) {
var array1 = data.split("\t");
var array2 = new Array();
for (var i = 0; i < array1.length; i++) {
array2.push(array1[i].split(","));
for (var j = 0; j < array2[i].length; j++) {
$('ul').append('<li>'+array2[i][j]+'</li>');
}
$('ul').append('<li><hr></li>');
}
}
});

Barrie Reader
- 10,647
- 11
- 71
- 139
-
1Don't know why but my understanding of the question is that it's a local csv file selected into a file input (but not submitted to the server). – Mihai Stancu Aug 30 '12 at 12:14
-
yeah sorry, the file will be uploaded by the user, it contains SKU and Price. These will then need to be passed via the ajax call to my webservice. which will then process the array/JSON as needed :) – thatuxguy Aug 30 '12 at 12:32
1
FileReference and FileAPI are libraries available it most modern browsers. After receiving the file and getting access to it as an array of characters you can apply regex or split.
An example:
// this first part is from FileAPI
// it allows you access to files that
// the user has selected
var file = $('#file').get().files[0];
data = getAsText(file);
// assuming you'll use a semicolon as separator
// and you're not forcing all fields to be quoted
var an_array = data.split(";");
$.post('script.php', an_array, function(response) {
// Do something with the response
}, 'json');
The getAsText()
function is not part of the API, I read it from here and there's no reason to copy it entirely.
Search Can I Use . com

Mihai Stancu
- 15,848
- 2
- 33
- 51
-
-
Of course not! Come on, you should be getting the hang of this already. [Can I Use FileAPI](http://caniuse.com/#search=fileapi). A workaround for this is to send the file to the server and split&save it server side or send it to the server and send it back to yourself (stupid right?). – Mihai Stancu Aug 30 '12 at 12:53
-
lol really hate IE! Was gonna use .NET to do this but i have been told to use jQuery and AJAX :( just to make things more difficult! – thatuxguy Aug 30 '12 at 13:19
-
You probably can get something out of ActiveX for IE and FileAPI for every other browser. It's just the file reading part that will differ. The rest will be common. – Mihai Stancu Aug 30 '12 at 13:24