问题描述
我在哪里,我使用生成从我的角度应用Excel文档的项目。我有电话从我的角度服务正在为我的WebAPI控制器如下:
I have a project where I am using NPOI to generate Excel document from my Angular application. I have the calls being made from my angular service to my webapi controller as follows:
function exportReportToExcel(report) {
return $http.post('reportlibrary/exportReport/', report, {
}).then(function (response) {
return response.data;
});
};
在控制器我提出以下电话
Within the controller I make the following call
[HttpPost]
public HttpResponseMessage ExportReport([FromBody]DTOs.Report report)
{
try
{
IReportPersistenceManager manager = ContainerConfigurator.Instance.Resolve<IReportPersistenceManager>();
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应用程序和previously我能够用有System.IO.File返回对象以及关闭流返回对象。迁移
This is a migration from an MVC app and previously I was able to return the object using System.IO.File to return the object as well as close the stream.
我从来没有与角这样做但是从我读过它似乎我可以删除的MemoryStream对象转换为bytearray并传递回客户端。
I've never done this with Angular but from what I have read it appears I can drop the memorystream object into a byteArray and pass that back to the client.
如果这是正确的做法我如何解开这个对象一旦回来角度的服务和控制器。
If this is the correct approach how to I unravel this object once it comes back to the angular service and controller.
这里的目标是让用户下载previously保存的报告为Excel文件。
The goal here is to allow the user to download a previously saved report as an Excel file.
我在正确的轨道上吗?有没有下载一个内存流对象,更有效的方法?如何传递这方面的浏览器?
Am I on the right track? Is there a more efficient way to download a memory stream object? How can I pass this context to the browser?
在此先感谢
推荐答案
我有一个类似的问题。我解决了,改变端点支持GET和呼叫从一个新的标签此方法。
I had a similar problem. I solved that changing the endpoint to support GET and calling this method from a new tab.
因此,从您的角度应用程序,你必须创建指向调用生成文档的方法路径中的新标签。您可以打开下一个code选项卡:
So from your angular app you have to create a new tab pointing to the path that calls the method that generate your document. You can open a tab with the next code:
$window.open(path)
我认为,在未来的链接,你可以有你需要的所有信息:
I think that in the next link you can have all the information that you need:
Download从使用的ASP.NET Web API方法文件angularjs
我希望它能帮助。
这篇关于如何通过angularJS和webaAPI2下载内存流对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!