Transferring a collection of pictures from an AngularJs directory

I am looking to streamline the process of uploading a list of images from a folder and storing them as bytestream in a database. Rather than selecting multiple files individually, I want to provide AngularJS with the entire folder containing the images. Below is the snippet of code responsible for this operation:

  $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);

  }

However, upon checking my log, I notice that only the last object contains image data, while the rest do not. This issue is depicted in the screenshot provided below: https://i.sstatic.net/RKGN2.jpg

It seems like there is an issue with how the image data is being handled. Any insights on why this problem is occurring and how it can be resolved would be greatly appreciated.

Answer №1

When converting an image to base64, it can take some time. Therefore, make sure 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, []);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Learn the techniques for toggling the visibility of a separate iframe through a hyperlink

<div class="category-tab" id="navg"><!--category-tab--> <div class="col-sm-15"> <ul class="nav nav-tabs"> <li class="active" ><a href="link" data-toggle="tab">name</a></li> <li ><a href="#link" dat ...

"Facing an issue with transferring $rootScope.updatedata data from the controller to the HTML view

I'm facing an issue trying to retrieve the value of $rootScope.updatedata from the controller and display it in the view using AngularJS. When I attempt to pass the data received from the server response to the view, it shows as null. Interestingly, w ...

What sets apart the Jquery Function Template from the Method Template?

Can someone explain the difference between Jquery Function Templates and Method Templates and how to properly utilize them? Below is a snippet of a Function Template and Method Template, but what sets them apart? Function Template:- (function($){ $.f ...

Implementing Vue.js pagination along with making http requests

My table features a pagination-nav component: <b-pagination-nav :link-gen="linkGen" limit="5" :number-of-pages="10" use-router> </b-pagination-nav> To retrieve the table content, I am utilizing the getTrades met ...

There was an issue encountered while parsing the JSON data - "SyntaxError: Unexpected token . was caught."

Encountering an error in Chrome while parsing JSON data. The JSON sample can be found at this link. It is valid JSON and the server is sending the correct Content-Type value (application/json). Uncaught SyntaxError: Unexpected token . Firefox shows a sli ...

Angular is capable of constructing an array

Whenever a user clicks on a tag in my app, I want to capture the id of the product clicked and store it in an array. This is how my view looks: <ion-view view-title='Products'> <ion-content> <div class="row"> <d ...

Utilizing pure JavaScript to dynamically fetch HTML and JavaScript content via AJAX, unfortunately, results in the JavaScript

I am trying to load an HTML file using AJAX and then execute a script. Here is the content of the HTML file I want to load: <div class="panel panel-body"> <h4>Personal Data</h4> <hr /> <span data-bind="editable: firs ...

Retrieve the size and position of the Kendo UI Splitter using the methods getSize()

I am currently working on a webpage using Kendo UI that allows users to customize the layout. I would like to implement a feature where the layout is saved so that the server can later send the user their last saved layout. One of the components I am usin ...

The this.setState method does not seem to be properly updating the nested array within the user object. However, when checking the same logic using console.log, the object

When the user selects multiple meals, they are stored in an array of objects inside the user object. Upon clicking submit, the code utilizes setState to update the user's meals as follows: submitAllMeals(arrayOfMeals) { this.setState({ u ...

Having trouble accessing information from Firebase Realtime Database within a React Native application

I am currently developing a React Native application that interacts with a Firebase database for reading and writing data. I have configured my Firebase permissions to allow read and write access: { "rules": { ".read": true, ...

How can I remove the script from Response.Write?

Instead of using Response.Write to test some values in code with an alert box, I tried writing dynamic javascript directly into the page. Even after reverting the code, rebuilding, and clearing all temp data from IE, the alert still pops up. I followed a ...

The intricacy of the jQuery each function and its interaction with $(this

EXPLANATION: I am working with two elements, each containing a set of options. The first element has options with db-ids as values, acting as parent categories. The second element is filled with options where the parent db-ids are their values and their o ...

Modifying attribute values within an AngularJS directive

I am working on a directive for an input element that adds leading zeros to a number when the user enters it. For example, '12' should be displayed as '0012' after the blur event. The directive takes a parameter to determine the desired ...

Displaying image titles when the source image cannot be located with the help of JavaScript or jQuery

I am currently facing an issue where I need to display the image title when the image is broken or cannot be found in the specified path. At the moment, I only have the options to either hide the image completely or replace it with a default image. $(&apo ...

Utilizing the .fadeOut('slow') method for CSS visibility manipulation

Here is a little JavaScript code snippet that I have: function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style. ...

The transmission of ContentType is unsuccessful

I'm having an issue with the code in my app. It seems that the content-type is not being sent. Is there a way to force it to be sent? $.ajax({ crossDomain: true, type: ...

How can I customize a currency directive in AngularJS using filters?

My goal is to enhance user experience by allowing input in custom currency values like '1.5M' instead of 1500000, and '1B' instead of 1000000000 on an input form dealing with large numbers. To achieve this, I've created a FormatSer ...

Modify the Bootstrap navbar color as you scroll through the page

While utilizing Bootstrap for a theme, I came across this interesting site: . The effect on the navbar caught my attention - it changes color when scrolling down and also modifies the color of elements. Appreciate any insights or advice. Thanks! ...

Having trouble getting the .replace() Javascript function to work on mobile devices?

I have a code snippet for implementing Google Analytics. Here it is: $(function () { $('.plan-choose-btn a').bind('click', function(e) { //ga load image <% String myaccGAEventUrl = trackGoogleAnalyticsEvent(requ ...

The issue of not being able to go fullscreen with a YouTube iframe nested within another iframe

I have a YouTube video embedded inside another iframe, but I am facing an issue where the fullscreen feature is not working even after enabling all the required attributes. If the video isn't displaying properly in the code snippet below, you can vie ...