title: FileReader read file undefined result
body: <p>I wish to read the contents of an upload  file into a Javascript variable.</p>
  
  <p>The program used to work using file.getAsBinary but this is now deprecated and needs to be updated to use FileReader()</p>
  
  <p>A form has a file upload selection an onsubmit runs a function uploadDB() in a javascript.</p>
  
  <p>The variable dbname is passed okay as is the file name from the upload selection
  I can see it with an alert or console.log.</p>
  
  <p>The final bfile variable to receive the file text is undefined. I have tried both readAsText and ReadAsBinary but bfile is undefined. The var declaration is in function uploadDB() and should be global to the inner function. If scope is somehow a problem how do I get the result in the bfile variable.</p>
  
  <p>Its probably simple but I cannot get it to work. Can someone  suggest something please.
  The html section is; </p>
  
  <pre><code>&lt;form onsubmit="uploadDB()"; method="post"&gt;
  Database &lt;input type=text name="dbName" id="dbName" size=20&gt;
  &lt;input type=file id="metaDescFile" size=50 &gt;
  &lt;input type=submit value="Submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The js script is similar to (extraneous stuff edited out);</p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
  &lt;!--
      var uploadDB = function() {
          var input = document.getElementById('metaDescFile');
          var fname = document.getElementById('metaDescFile').value;
          var file=input.files[0];
          var bfile;
  
          reader = new FileReader();
  
          reader.onload = function(e) {  bfile = e.target.result }
          reader.readAsText(file);
          alert(bfile);   // this shows bfile as undefined
  
  
          // other stuff
      }
  </code></pre>