简体   繁体   English

FileReader 删除多个文件时只读取一个文件

[英]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下面的代码确实记录了每次迭代(console.log(i)),但只读取和记录最后一个文件,无论是读取为 dataURL 还是 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提前感谢

use let instead of var to properly scope the variable to the loop:使用let而不是var正确 scope 循环变量:

for (let i = 0; i < files.length; i++) { // Use let here

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

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

}

Didn't you forget the multiple attribute on the input element?是不是忘记了input元素上的multiple属性?

You should use let instead of var for reader variable, since var is function scoped.您应该使用let而不是var作为reader变量,因为var的范围是 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM