title: Download excel file using webapi and angularjs
body: <p>I'm trying to download a closedXml excel file in a webapi/angularjs application. </p>
  
  <p>I'm returning the data from the webapi controller on the server using:</p>
  
  <pre><code> HttpResponseMessage result = new HttpResponseMessage();
   result = Request.CreateResponse(HttpStatusCode.OK);
   MemoryStream stream = GetStream(workbook);
   result.Content = new StreamContent(stream);
   result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
   result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
   {
       FileName = "Download.xlsx"
   };
   return result;
  </code></pre>
  
  <p>and then saving it on the client using:</p>
  
  <pre><code>$scope.openExcel =  function (data, status, headers, deferred) {
          var type = headers('Content-Type');
          var disposition = headers('Content-Disposition');
          if (disposition) {
              var match = disposition.match(/.*filename=\"?([^;\"]+)\"?.*/);
              if (match[1])
                  defaultFileName = match[1];
          }
          defaultFileName = defaultFileName.replace(/[&lt;&gt;:"\/\\|?*]+/g, '_');
          var blob = new Blob([data], { type: type });
  
          saveAs(blob, defaultFileName);
  </code></pre>
  
  <p>Excel says the file is in a different format than specified by the extension and then doesn't open properly.</p>