Descargar datos como file de Excel usando API web y angular JS

Tengo un escenario para download el file de la aplicación web que utiliza a) Angular JS como front end b) Web api como server y el browser es IE9.

He intentado muchos plugins para convertir HTML table a Excel, csv pero ninguno de ellos funcionó para IE9. Así que he decidido generar el file en la API web y downloadlo en el cliente. Pero eso tampoco funciona. ¿Alguien puede compartir un ejemplo de trabajo para este escenario? Código Angular JS:

var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope, exportToExcelService) { $scope.export = function () { exportToExcelService.download().success( function (response) { }) .error(function (response, status) { }); } }). factory('exportToExcelService', function ($http) { var sampleAPI = {}; sampleAPI.download = function () { return $http({ method: 'POST', url: 'api/Sample/download' }); } return sampleAPI; }); 

Código de controller web APi:

 [HttpPost] public HttpResponseMessage download() { List<Record> obj = new List<Record>(); obj = RecordInfo(); StringBuilder str = new StringBuilder(); str.Append("<table border=`" + "1px" + "`b>"); str.Append("<tr>"); str.Append("<td><b><font face=Arial Narrow size=3>FName</font></b></td>"); str.Append("<td><b><font face=Arial Narrow size=3>LName</font></b></td>"); str.Append("<td><b><font face=Arial Narrow size=3>Address</font></b></td>"); str.Append("</tr>"); foreach (Record val in obj) { str.Append("<tr>"); str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.FName.ToString() + "</font></td>"); str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.LName.ToString() + "</font></td>"); str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + val.Address.ToString() + "</font></td>"); str.Append("</tr>"); } str.Append("</table>"); HttpResponseMessage result = null; // serve the file to the client result = Request.CreateResponse(HttpStatusCode.OK); byte[] array = Encoding.ASCII.GetBytes(str.ToString()); MemoryStream mem = new MemoryStream(array); result.Content = new StreamContent(mem); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Data.xls" }; return result; } public List<Record> RecordInfo() { List<Record> recordobj = new List<Record>(); recordobj.Add(new Record { FName = "Smith", LName = "Singh", Address = "Knpur" }); recordobj.Add(new Record { FName = "John", LName = "Kumar", Address = "Lucknow" }); recordobj.Add(new Record { FName = "Vikram", LName = "Kapoor", Address = "Delhi" }); recordobj.Add(new Record { FName = "Tanya", LName = "Shrma", Address = "Banaras" }); recordobj.Add(new Record { FName = "Malini", LName = "Ahuja", Address = "Gujrat" }); recordobj.Add(new Record { FName = "Varun", LName = "Katiyar", Address = "Rajasthan" }); recordobj.Add(new Record { FName = "Arun ", LName = "Singh", Address = "Jaipur" }); recordobj.Add(new Record { FName = "Ram", LName = "Kapoor", Address = "Panjab" }); return recordobj; } 

Encontré la solución usando el siguiente código y funciona como un amuleto. Solo necesitamos agregar window.location.href.

 app.controller('myCtrl', function ($scope, exportToExcelService,$location) { $scope.export = function () { exportToExcelService.download().success( function (response) { window.location.href = 'api/sample/download' }) .error(function (response, status) { var str = 'Hello' }); } }).