Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  58] [ 1]  / answers: 1 / hits: 19213  / 10 Years ago, fri, october 17, 2014, 12:00:00

I'm trying to do something fairly simple I think but I'm missing something. I've very new to Javascript. I'm trying to read a CSV file in to an array (in my code below I'm simply trying to output the data to an alert box). I keep getting an error access denied.



function readTextFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open(GET, file, true);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
var allText = rawFile.responseText;
alert(allText);
}
}
}
rawFile.send(null);
}


I suspect there is an issue with where I have the csv file located? Due to restrictions with our CMS I can only reference the file like this www.example.com/csvfile.csv.



Any help would be greatly appreciated.


More From » csv

 Answers
43

Here is sample code for reading csv file into array



var request = new XMLHttpRequest();  
request.open(GET, url, false);
request.send(null);

var csvData = new Array();
var jsonObject = request.responseText.split(/r?n|r/);
for (var i = 0; i < jsonObject.length; i++) {
csvData.push(jsonObject[i].split(','));
}
// Retrived data from csv file content
console.log(csvData);


Here is the working fiddle example: http://jsfiddle.net/BdCnm/450/


[#69098] Tuesday, October 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;