title: Javascript Filereader - File Type Checking
body: <p>I'm currently trying to implement a new HTML5 driven upload site. Out of security reasons I was usually quite restrictive regarding file types to be uploaded, so usually only ZIP files were accept from the back end. As there seem to be many people around that seem to have difficulties using ZIP, I thought I'd give it a try and do something as follows:</p>
  
  <ul>
  <li>Select files to be uploaded</li>
  <li>Read 4 first bytes of each file to determine if it's a ZIP file (it's not fool proof, but never mind that).</li>
  <li>Upload ZIP files directly</li>
  <li>Take all the other files and create a ZIP archive on the fly and upload that newly created archive.</li>
  </ul>
  
  <p>The whole thing works well, except for the file type checking:</p>
  
  <pre><code>//'files' is an array of files retrieved from &lt;input type='file'...for instance
      function checkFileType(files, callback) {
          for (var i = 0; i &lt; files.length; i++) {
  
              var fileReader = new FileReader();
  
              fileReader.onloadend = function (e) {
                  var arr = (new Uint8Array(e.target.result)).subarray(0, 4);
                  var header = "";
                  for (var j = 0; j &lt; arr.length; j++) {
                      header += arr[j].toString(16);
                  }
  
                  if (header == '504b34')
                      ;
                  else
                      callback("something");
  
              };
  
  
              var blob = files[i].slice(0, 4);
              fileReader.readAsArrayBuffer(blob);
          }
      }
  </code></pre>
  
  <p>I'm using "slice" here to only read the 4 first bytes (in case the file is huge, I don't want to mess up memory). The only thing that troubles me: Because of the slicing I lose the reference to the actual file in the array. I need to remove files that are being detected as ZIP from the array and start uploading them on the fly.</p>
  
  <p>Afterwards, all remaining files in the array shall be zipped. My approach doesn't seem to work because of that missing 'reference'...</p>