I'm building an ajax file uploader, which is possible thanks to the new FormData interface. It works if I use the original file, but if I convert the file to a binary string and then to a blob, the resulting file is corrupted. What am I doing wrong?
<!DOCTYPE html>
<html>
  <body>
    <form method=post enctype=multipart/form-data id=form>
      <input id=file type=file name=file>
      <button id=1>1</button>
      <button id=2>2</button>
    </form>
    <script>
      var d = document;
      function $(id) { return d.getElementById(id); };
      function xhr(fd) {
        var x = new XMLHttpRequest();
        x.open('post', '/', true);
        x.send(fd);
      };
      $(1).addEventListener('click', function(e) {
          e.preventDefault();
          var file = $('file').files[0];
          var fd = new FormData();
          fd.append('file', file);
          xhr(fd);
        }, false
      );
      $(2).addEventListener('click', function(e) {
          e.preventDefault();
          var file = $('file').files[0];
          var fr = new FileReader();
          fr.onload = function(e) {
            var bb = new (window.BlobBuilder ||
              window.MozBlobBuilder || window.WebKitBlobBuilder)()
            bb.append(e.target.result);
            var b = bb.getBlob(file.type);
            var fd = new FormData();
            fd.append('file', b);
            xhr(fd);
          };
          fr.readAsBinaryString(file);
        }, false
      );
    </script>
  </body>
</html>
Blob BlobBuilder FileReader FormData
edited to include links
 
    