1

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;
};
3
  • Can you show the code of the method getReader(deferred, scope) ? I guess it is this method which resolve the promise. Commented Jan 6, 2016 at 15:09
  • all the code is in the plunkr. i can paste it her but no reable Commented Jan 6, 2016 at 15:10
  • ok, sorry. I will have a look at it. Commented Jan 6, 2016 at 15:10

1 Answer 1

1

Try this:

var readAsText = function (file, scope) {
   var deferred = $q.defer();
   var reader = getReader(deferred, scope);
   reader.readAsText(file);
   // then will return another promise with content and file available
   return deferred.promise.then(function(result){
        return {
            content: result,
            file: file
        };
   });
};

You can use it then as follows:

readAsText(file, $scope).then(function(result){
    var content = result.content;
    var file = result.file;
    //...
})
Sign up to request clarification or add additional context in comments.

2 Comments

how to use it then please ?
it work! i will update and valid your solution after some try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.