简体   繁体   English

无法使用HTML FileReader从多个文件读取数据

[英]Unable to read data from multiple files using HTML FileReader

<input type="file" id="input" multiple onchange="handleFiles(event.target)">
<script language="javascript" type="text/javascript">
     <!--

     function handleFiles(input)
     {
          for (i = 0; i < input.files.length; i++)
          {
              var reader = new FileReader();
              reader.onload = function() 
              {
                  alert(reader.result)
              };
              reader.readAsText(input.files[i]);
          }
      }

     //-->
</script>

I am trying to display the contents of some files that I upload. 我正在尝试显示我上传的某些文件的内容。 It works fine if I select a single file, but if I select multiple files, only the content of one file is displayed, rest all are blank. 如果选择一个文件,效果很好,但是如果选择多个文件,则仅显示一个文件的内容,其余全部为空白。 What am I doing wrong? 我究竟做错了什么?

You just need to amend it slighty to do the following: 您只需要稍微修改一下即可进行以下操作:

<input type="file" id="input" multiple onchange="handleFiles(event.target)">
<script language="javascript" type="text/javascript">
function handleFiles(input)
{
    for (var i = 0; i < input.files.length; i++) { //for multiple files          
        (function(file) {
            var name = file.name;
            var reader = new FileReader();  
            reader.onload = function(e) {  
                // get file content  
                var text = e.target.result; 
                alert(text)
            }
            reader.readAsText(file, "UTF-8");
        })(input.files[i]);
    }
}
</script>

Reference: https://stackoverflow.com/a/9815648/3088780 参考: https : //stackoverflow.com/a/9815648/3088780

I think jquery help you easily 我认为jQuery可以轻松帮助您
HTML: HTML:

<script src="jquery-2.2.0.min.js"></script>
<input type="file" multiple="multiple" />
<ul id="output">
</ul>

Jquery: jQuery:

$('input:file[multiple]').change(
    function(e){
        console.log(e.currentTarget.files);
        var numFiles = e.currentTarget.files.length;
            for (i=0;i<numFiles;i++){
                fileSize = parseInt(e.currentTarget.files[i].size, 10)/1024;
                filesize = Math.round(fileSize);
                $('<li />').text(e.currentTarget.files[i].name).appendTo($('#output'));
                $('<span />').addClass('filesize').text('(' + filesize + 'kb)').appendTo($('#output li:last'));
            }
    });

reference from : https://stackoverflow.com/a/7719334/5566169 参考来自: https : //stackoverflow.com/a/7719334/5566169

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

相关问题 使用FileReader()从HTML表单读取图像 - Using FileReader() to read an image from HTML form 我正在使用FileReader API读取多个文件并分成多个部分 - I'm using FileReader API to read multiple files and breaking into parts 使用FileReader API从本地html页面读取文本文件 - Read text file from local html page using the FileReader API 无法使用 html 从 firebase 读取数据 - unable to read data from firebase using html 使用.map()使用FileReader读取多个文件 - Reading multiple files with FileReader using .map() 使用FileReader在Javascript中同步读取多个文件 - Reading multiple files synchronously in Javascript using FileReader FileReader不会从多个文件渲染图像 - FileReader does not render the image from multiple files 使用FileReader()读取文件以从图像文件生成md5哈希字符串的正确方法? - Proper way to read a file using FileReader() to generate an md5 hash string from image files? 无法使用 Lightning 组件中的 FileReader 对象读取和上传文件 - Unable to read and upload file using FileReader object in Lightning component Javascript 如何使用 FileReader.onload 从多个 json 文件加载内容? - Javascript how to load contents from multiple json files using FileReader.onload?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM