Q:

Uploading list of images from a folder AngularJs

I want to upload list of images from a folder and stored them as bytestream in database. I want to give angularjs the  folder containing the images instead of selecting multiple files . The part of the code responsible is given below.
  $scope.uploadMultipleQuestions = function(e) {
      var questionList = []
      var difficultyLevel = vm.question.difficultyLevel;
      var theFiles = e.files;
      for (var i = 0; i < theFiles.length; i++) {
          var ques = {};
          ques.questionString = theFiles[i].name;
          DataUtils.toBase64(theFiles[i], function(base64Data) {
              $scope.$apply(function() {
                  ques.questionImage = base64Data;
              });
              [![enter image description here][1]][1]
          });
          ques.questionImageContentType = theFiles[i].type;
          ques.questionString = theFiles[i].webkitRelativePath.split("/")[1];

          questionList.push(ques);
          Question.uploadMultipleQuestions(questionList);
      }
      for (var i = 0; i < questionList.length; i++) {
          console.log(questionList[i]);
      }
      //Question.uploadMultipleQuestions(questionList);

  }

But the problem is I am getting the following details in my log.(Screenshot attached below)
As you can see only the last object contains image data whereas none of the others have any image content.
Let me know why this problem is coming and how to solve the same.

A:

It take a while to convert image to base64, so you have to upload your image after ques.questionImage is filled.  
var uploadMultipleQuestions = function(files, i, output) {

    if (files.length == i) {
        for(var j=0;j<output.length;j++)
            console.log(output[j]);

        return Question.uploadMultipleQuestions(output);
    }

    DataUtils.toBase64(files[i], function(base64Data) {

        output.push({
            questionString: files[i].name,
            questionImageContentType: files[i].type,
            questionString: files[i].webkitRelativePath.split("/")[1],
            questionImage: base64Data
        });

        uploadMultipleQuestions(files, i+1, output);

    });
}

$scope.uploadMultipleQuestions =function(e){
    var theFiles = e.files;
    uploadMultipleQuestions(theFiles, 0, []);
}

