I make an interface with an multiple upload csv file ( done ).
This CSV files have to be loaded to the navigator client with a custom fileReader service who use $q (done) , then parsed with ngPapaParser (done) and displayed in the view with ngTable (done but not in the example to simplify).
This is a plunkr showing the error i have : http://plnkr.co/edit/YVvitZ2yfxjJw76n6t9F?p=preview
The problem is need to get the file name inside the fileReader.readAsText promise but i don't know how to get it.
controller('MainCtrl', function($scope, Papa,fileReader) {
$scope.parsedResults = [];
$scope.getFiles = function (files) {
$scope.parsedResults = [];
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
console.log(f.name); // ok her
fileReader.readAsText(f, $scope).then(function(resultText) {
Papa.parse(resultText, {
header: true,
// worker: true,
skipEmptyLines: true,
complete: function(result) {
var cols= [];
for(var i in result.meta.fields){
cols.push({
field: result.meta.fields[i] , title: result.meta.fields[i].toLowerCase() , show: true
});
}
console.log(f.name); // undefined her in the callback
//I WANT THE FILENAME HER !!!
var io = { 'filename': 'f.name unknow' ,'cols':cols ,'tableParams': result.data } ;
$scope.parsedResults.push(io);
}
});
});
}
};
});
how to make the following function returning a promise and the file ?
var readAsText = function (file, scope) {
var deferred = $q.defer();
var reader = getReader(deferred, scope);
reader.readAsText(file);
// how to return an object her, or the deferred promise and the file ?
return deferred.promise;
};
getReader(deferred, scope)? I guess it is this method which resolve the promise.