在AngularJS中保存来自API控制器的Excel文件

我试图从我的Web API控制器(Asp.net MVC 6)的方法下载文件。 所以,我的文件是在api控制器中创build的,我想用angularjs下载我的文件,已经以excel格式。 但是,我不知道该怎么办。

这里是我的方法在我的api控制器:(它的工作原理,我已经使用这个方法和类在另一个项目Asp.net没有angularj)

#region downloadFile [Route("Download")] public ActionResult Download() { string companyName = Context.Session.GetString("companyName") as string; string fileDownloadName = Context.Session.GetString("fileDownloadName") as string; string baseFolder = System.IO.Path.Combine("C:\\Temp"); string folder = System.IO.Path.Combine(baseFolder, companyName); TempFile tempFile = new TempFile(folder, fileDownloadName); tempFile.FindFileInDirectory(); return File(tempFile._pathDirectory + "\\"+tempFile._fileName, "Application/" + tempFile._fileExt, tempFile._fileName); } #endregion 

这个方法返回一个excel文件。 现在,我会下载这个文件,它是用一个http请求在angularjs中发送的。

我试图从fileSaver.js中使用saveAs方法。 我已经在我的项目中添加了js,但是当我想要使用它时,方法总是未定义的。

  var requestDownloadFile = $http({ url: '/api/customers/Download' , responseType: 'arraybuffer' , headers: { 'contentType': 'application/json' , 'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' } }); requestDownloadFile.success(function (data) { saveAs(data, 'test.xlsx'); }); 

我也试图在我的成功方法中使用它:

  var blob = new Blob([data], { type: "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet" }) var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); 

它运行一个下载文件,但该文件已损坏。

所以这不是真正有效的,因为我已经在API中创build了一个文件,我试图恢复一个arrayBuffer,但我被封锁,所以我尝试。

所有支持是受欢迎的。 谢谢

所以,我复杂的生活。 我只需要成功地做到这一点:

  window.open('/api/customers/Download', '_blank', ''); window.open('link/to/method/api', '_blank', '');