Read a file from upload

Hi everibody. I used this code to load a file from local disk

 toolbar.loadXML("data/toolbar.xml", function(){
				
				toolbar.addText('cb', 0, '<input type="file" id="fileButton" style="width:75px">');

Now I want to read the content of file.

How can I open and show the uploaded file?
How can I manage the event on file uploaded ?

I would like to use this function:


		function readSingleFile(evt) {
			//Retrieve the first (and only!) File from the FileList object
			var f = evt.target.files[0]; 

			if (f) {
			  var r = new FileReader();
			  r.onload = function(e) { 
				  var contents = e.target.result;
				alert( "Got the file.n" 
					  +"name: " + f.name + "n"
					  +"type: " + f.type + "n"
					  +"size: " + f.size + " bytesn"
					  + "starts with: " + contents.substr(1, contents.indexOf("n"))
				);  
			  }
			  r.readAsText(f);
			} else { 
			  alert("Failed to load file");
			}
		}

Hi I solved my problem.

First I managed the onchange event directly in the input tag

          toolbar.loadXML("data/toolbar.xml", function(){
				
				toolbar.addText('cb', 0, '<input type="file" id="fileButton" style="width:75px" onchange="readFiles();">');
			});   

and here is the function to read file contents

function readFiles() {
			// Get input element
			myFileList = document.getElementById("fileButton");

			//Retrieve the first (and only!) File from the FileList object
			var file = myFileList.files[0]; 
			if (file) {

				// loop through files property, using length to get number of files chosen
				for (var i = 0; i < myFileList.files.length; i++) {
					// display them in the div
					//document.getElementById("display").innerHTML += "<br/>" + myFileList.files[i].name ;
					var file = myFileList.files[i];
					alert(file.name);
					alert(file.size);
					
					var r = new FileReader();
					r.onload = function() { 
						var contents = r.result;
						console.log(r.result.substring(0, 200));
						alert(r.result.substring(0, 200));
					}
					r.readAsText(file);
				}
			} else { 
			  alert("Failed to load file");
			}
	
		}

Hm. Good solution.