0

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)enter image description here

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.

1

1 Answer 1

1

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, []);
}
Sign up to request clarification or add additional context in comments.

4 Comments

@TahseemAhmad , YW, if it works please hit the accept button.
Now what is happening is that questionString and questionImageContentType is coming same for all the files.What should i do to get the individual file names?
Thanks man ,.One more question ,What do i have to do to become a expert like you ? :P
@TahseemAhmad , :)) just working and reading bro. whenever you digging stackoverflow for your question try to read all answers, no matter it's related to your question or not. And check the source code of open source modules you are using. some of them are awesome!

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.