I am trying to use baby parse (branch off of Papa Parse) to take a csv file and parse the data to Json. The csv file I am testing is very straight forward:
Column1,Column2,Column3
1,2,3
3,2,1
So far I am successfully loading the file using ng-file-upload to load the csv file via the browser.
I am now trying to use baby parse to take this file and convert the data to JSON.
Below is what I have so far:
The controller
// Watch for a csv file being uploaded.
$scope.$watch(function() {
return $scope.file
}, function(){
$scope.upload($scope.file);
});
$scope.upload = function(file){
if(file){
Upload.upload({
url: 'api/admin/uploadCsv',
method: 'POST',
data: {userId: $scope.user._id},
file: file
}).progress(function(evt){
console.log("firing");
}).success(function(data){
}).error(function(error){
console.log(error);
})
}
};
On file upload the upload function is called and calls the server controller. The file uploaded is passed to the upload function which I am then attempting to pass to the parse method.
module.exports.uploadCsv = function(req, res){
var file = req.files.file;
var userId = req.body.userId;
parsed = babyparse.parse(file.path, babyParseConfig);
console.log("data is " + (JSON.stringify(parsed.data)));
...
The console.log outputs the following:
data is {"data":[],"errors":[],"meta":{"delimiter":",","linebreak":"\n","aborted":false,"truncated":false,"fields":["..\\App\\uploads\\565b8feecddbb1e41b7aa839test.csv"]}}
I can not understand why data:[] is empty given what the csv file looks like.
Using the answer provided here ( how to give file name a input in baby parser ), I have got the parser working. Below is what I ended up with:
var uploadcontent = fs.readFileSync(targetPath, { encoding: 'binary' });
var parsed = babyparse.parse(uploadcontent);
console.log((JSON.stringify(parsed)));
Note that uploadContent is the path to the directory which I am saving the file to rather than the file itself.
The above outputs the following
{"data":[["Column1","Column2","Column3"],["1","2","3"],["3","2","1"]],"errors":[],"meta":{"delimiter":",","linebreak":"\r\n","aborted":false,"truncated":false}}
The above can be refined to something like console.log((JSON.stringify(parsed.data)));
to get just data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.