Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm trying to read the contents of multiple files from JavaScript. One file works fine but if I select multiple files, onload seems to be called the appropriate number of times but only the contents of the last files are displayed. Other files are displayed as blank. Any ideas would be great.

I'm guessing the file may not have been read before I try and display but I'm not sure why. Any help would be great. Thanks,

Brian

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>File API</title>
    <script>
	function processFiles()
	{
		
    	var filelist = document.getElementById('files').files;

    	
		for(var i=0; i<filelist.length; i++)
		{
			
			var file = filelist[i];
			var textType = /text.*/;
			

			if (file.type.match(textType)) 
			{			
				var reader = new FileReader();
				reader.>
</head>
<body>
Select a text file: 
<input type="file" multiple id="files" onchange="processFiles()" />
</body>
</html>
Posted
Updated 7-Feb-15 5:25am
v4
Comments
Sergey Alexandrovich Kryukov 7-Feb-15 9:53am    
What's the difference, one file or more? You don't show where you "display" the file. Maybe you write the data to the same place. :-)
—SA

Try this:
XML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type='file' accept='text/plain' onchange='openFile(event)'><br>
<ul id='filelist'></ul>
<script>
  var openFile = function(event) {
    var input = event.target;

    var reader = new FileReader();
    reader.onload = function(){
      var text = reader.result;
      var ul = document.getElementById("filelist");
      var li = document.createElement("li");
      li.innerHTML = reader.result;
      ul.appendChild(li);
    };
    reader.readAsText(input.files[0]);
  };
</script>

</body>
</html>

Read more: FileReader[^]
 
Share this answer
 
v2
Comments
BrianHamilton 7-Feb-15 11:14am    
The code will not allow me to open multiple files - if I add the multiple attribute it will still only display the contents of the last file selected rather than the contents of all files.
Peter Leow 7-Feb-15 11:34am    
test it yourself at http://js.do/code/50529
XML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>File API</title>
    <script>
    function processFiles()
    {
        var filelist = document.getElementById('files').files;
        for(var i=0; i<filelist.length; i++)
        {
            writefiles(filelist[i]);
        }
    }

    function writefiles(file)
    {
        var reader = new FileReader();
        reader.onload = function()
        {
            document.getElementById('output').innerText += reader.result;
        }
        reader.readAsText(file, "UTF-8");
    }
    </script>
</head>
<body>
Select a text file:
<input type="file" accept='text/plain' multiple id="files" onchange="processFiles()" />
<pre id="output"></pre>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900