JavaScript Multiple file reading with FileReader and readAsArrayBuffer multiple file reading in java,python multiple file reading,reading

I'm trying to read and add multiple files to an array. I've already found out that readAsArrayBuffer is a asynchronous function, so i need to wait for previous uploads to end. I tried to use callbacks but i failed.

<form enctype="multipart/form-data">
  <input id="file-input" type="file" multiple="" accept="image/*">
</form>
<div id="upload-list"></div>

Here's js file:

var fileList = [];
$(document).ready(function() {
  function addFiles(files) {
    for (var i = 0; i < files.length; i++) {
      var file = files[i];
      var reader = new FileReader();
      reader.onloadend = (function(file) {
        return function() {
          fileList.push(reader.result);
          $('#upload-list').append('<div class="upload-list-item">' + file.name '</div>');
        }
      })(file);
      reader.readAsArrayBuffer(file);
      console.log(fileList);
    }
  }
  $('#file-input').on('change', function(e) {
    addFiles(e.target.files);
  });
}

So now i have new FileReader each iteration, i got rid from Failed to execute 'readAsArrayBuffer' on 'FileReader': The object is already busy reading Blobs error, now i have the same files in my array, although I selected different.

google chrome dev

Answer:1

Your issue is that reader is going to be clobbered - using .forEach will mean each iteration is safe in its own closure - no need for IIFE kludges either

function addFiles(files) {
    // files is not a regular Array
    [].forEach.call(files, function(file) {
        var reader = new FileReader();
        reader.onloadend = function() {
            fileList.push(reader.result);
            $('#upload-list').append('<div class="upload-list-item">' + file.name + '</div>');
        }
        reader.readAsArrayBuffer(file);
    });
}

However this does not guarantee any particular "order" of the $('#upload-list').append - because asynchronous code is asynchronouos

Using Promises (which CAN be polyfilled for older browsers) you could do something like

function addFiles(files) {
    return Promise.all([].map.call(files, function (file) {
        return new Promise(function (resolve, reject) {
            var reader = new FileReader();
            reader.onloadend = function () {
                resolve({ result: reader.result, file: file });
            };
            reader.readAsArrayBuffer(file);
        });
    })).then(function (results) {
        results.forEach(function (result) {
            $('#upload-list').append('<div class="upload-list-item">' + result.file.name + '</div>');
        });
        return results;
    });
}

This will guarantee the order that $('#upload-list').append is called

Answer:2

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

I am facing issues in writing into iframe, I am able to identify the iframe but I am not able to write anything into the frame. Whenever I try to write into the frame I am getting below error. org....

  1. need help writing a paper
  2. need help writing a book
  3. need help writing an essay
  4. need help writing a song
  5. need help writing a resume
  6. need help writing a letter
  7. need help writing a poem
  8. need help writing a cover letter
  9. need help writing vows
  10. need help writing my resume
  11. need help writing a sentence
  12. need help writing business plan
  13. need help writing rap lyrics
  14. need help writing a rap song
  15. need help writing my book
  16. need help writing a thank you note
  17. need help writing research paper
  18. need help writing my vows
  19. need help writing dissertation
  20. need help writing a love letter

I have a computed method: computed: { currentPosition () { if(this.get_local_storage_state()){ return this.lastLocation } if (this.currentRestaurant) { return this....

I have a computed method: computed: { currentPosition () { if(this.get_local_storage_state()){ return this.lastLocation } if (this.currentRestaurant) { return this....

This code form is working, but it doesn't call the ajax form chat_process.php. I need to call the ajax form and alert a message. var html = ""; for(...) { html += '<form class="...

This code form is working, but it doesn't call the ajax form chat_process.php. I need to call the ajax form and alert a message. var html = ""; for(...) { html += '<form class="...

  1. ajax form not working
  2. ajax form not working mvc
  3. ajax form submit not working
  4. ajax form serialize not working
  5. ajax form submit not working mvc
  6. ajax form preventdefault not working
  7. ajax form success not working
  8. ajax form post not working
  9. ajax form validation not working
  10. ajax form plugin not working
  11. ajax beginform not working
  12. ajax acf_form not working
  13. django ajax form not working

I am using the demo code for intlTelInput provided here I am able to get the dial code using following var intlNumber = $("#phone").intlTelInput("getSelectedCountryData"); But i am not ...

I am using the demo code for intlTelInput provided here I am able to get the dial code using following var intlNumber = $("#phone").intlTelInput("getSelectedCountryData"); But i am not ...