如何通过angularJS和webaAPI2下载内存流对象

我有一个项目,我使用NPOI从我的Angular应用程序生成Excel文档。 我可以通过我的角度服务调用我的webapi控制器,如下所示:

function exportReportToExcel(report) { return $http.post('reportlibrary/exportReport/', report, { }).then(function (response) { return response.data; }); }; 

在控制器内,我进行以下调用

 [HttpPost] public HttpResponseMessage ExportReport([FromBody]DTOs.Report report) { try { IReportPersistenceManager manager = ContainerConfigurator.Instance.Resolve(); MemoryStream ms = new MemoryStream(); //we have to pass to the NOPI assemble file type as well as file name //since we only deal with excel for now we will set it but this could be configured later. long id = report.ReportId; string mimeType = "application/vnd.ms-excel"; string filename = "unknown"; manager.ExportDataToExcel(id, (name, mime) => { mimeType = mime; filename = name; return ms; }); ms.Position = 0; var response = new HttpResponseMessage(); response.Content = new ByteArrayContent(ms.ToArray()); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel"); return (response); } catch (Exception) { //error return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest); } } 

这是从MVC应用程序的迁移,之前我能够使用System.IO.File返回对象以返回对象以及关闭流。

我从来没有用Angular做过这个,但是从我看过的内容看来,我可以将memorystream对象放入byteArray并将其传递回客户端。

如果这是正确的方法,一旦它回到角度服务和控制器,如何解开该对象。

这里的目标是允许用户将以前保存的报告下载为Excel文件。

我是在正确的轨道上吗? 有没有更有效的方法来下载内存流对象? 如何将此上下文传递给浏览器?

提前致谢

我有类似的问题。 我解决了更改端点以支持GET并从新选项卡调用此方法的问题。

因此,从角度应用程序中,您必须创建一个新选项卡,指向调用生成文档的方法的路径。 您可以使用下一个代码打开一个选项卡:

 $window.open(path) 

我认为在下一个链接中,您可以获得所需的所有信息:

使用AngularJS从ASP.NET Web API方法下载文件

我希望它有所帮助。