I'm creating a webapp which features uploads of .csv file.
Instead of uploading the whole file on my server, then adding every lines in a table thanks to PHP, I'd like to parse the file in Angular, then add parsed lines (thanks to Restangular). This way, I should be able to retrieve some "errors" before uploading them (or not).
In a view, I've created a simple file input, with a directive called file (source : http://angularjstutorial.blogspot.fr/2012/12/angularjs-with-input-file-directive.html)
<input type="file" data-file="param.file" />
The filedirective :
app.directive('file', function(){
return {
scope: {
file: '='
},
link: function(scope, el, attrs){
el.bind('change', function(event){
var files = event.target.files;
var file = files[0];
scope.file = file ? file.name : undefined;
scope.$apply();
});
}
};
});
This way, I can retrieve when the user chooses a file. Unfortunately, I'm getting only the file name.
I'd like to parse the csv in a string, which i'll split thanks to this filter (source : parsing CSV in Javascript and AngularJS) :
app.filter('csvToObj',function(){
return function(input){
var rows=input.split('\n');
var obj=[];
angular.forEach(rows,function(val){
var o=val.split(';');
obj.push({
designation:o[1],
...
km:o[11]
});
});
return obj;
};
});
How can I retrieve the data in the csv file provided by the input instead of the file name ?
Thanks in advance.