title: Upload excel file from using angular js and WebAPI
body: <p>I am trying to upload an excel file using Angular js, send the string data to Webapi and then convert it to file. However when i try to open this file in explorer, it says file is corrupted.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div ng-click="chooseDocument()" class="btn btn-primary "&gt;
      &lt;span class="glyphicon glyphicon-file"&gt;&lt;/span&gt;&amp;nbsp;Upload
  &lt;/div&gt;
  &lt;input id="chooseDocument" type="file" class="form-control" style="display: none;" onchange="angular.element(this).scope().addDocument(this)" /&gt;
  </code></pre>
  
  <p>Angular Js:</p>
  
  <pre><code> $scope.chooseDocument = function () {
      var dialog = $("#chooseDocument");
      dialog.trigger("click");
  }
  
  $scope.addDocument = function (dialog) {
      var files = dialog.files;
      var reader = new FileReader();
  
      function readFile() {
          var file = files[0];
          reader.onloadend = function () {              
              uploadExcel(file.name, reader.result);
          }
          reader.readAsDataURL(file);
      }
      readFile(0);
  }
  
  function uploadExcel(filename, file) {
      $http.post(getUrl('api/User/UploadExcel'), { DocumentName: filename, DocumentContent: file }).then(function (response) {
        //do something
  
      }, function (errors) {
          //do Something
      });
  
  };
  </code></pre>
  
  <p>WebAPI:</p>
  
  <pre><code> File.WriteAllBytes(@"c:\MyFile\" + document.DocumentName, Encoding.ASCII.GetBytes(document.DocumentContent));
  
   document.DocumentContent has value "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,UEsDBBQABgAIAAAAIQC82fymuQEAACIIAAATAAgCW0NvbnRlbnRf..."
  </code></pre>