Search code examples
javascriptfilereader

FileReader reads only one file when multiple dropped


I am coding and form with and struggle tor ead each file. What i am getting is onlz one file being read.

Code below does log each iteration (console.log(i)), but does read and log only the last file, no matter what if read as dataURL or as Text

fileInput.on('change',function(){

    var files= fileInput.prop('files');
    console.log(files);

    for(var i = 0; i < files.length; i++){

       var reader = new FileReader();
      console.log(i);
      reader.onload = function(){

          console.log(reader.result);
      }
      reader.readAsDataURL(files[i]);

    }

  });

I need each of multiple file to be logged in console. Thanks in advace


Solution

  • You should use let instead of var for reader variable, since var is function scoped.