【发布时间】:2019-01-25 13:19:21
【问题描述】:
以下是生成 Excel 的 C# WEB API 代码:
public class FileExportController : ApiController
{
[HttpGet]
public HttpResponseMessage Get()
{
var callerContext = CallerContext.DefaultCallerContext;
ReportingInput userInput = new ReportingInput();
userInput.ClientOneCode = "AVON";
string handle = Guid.NewGuid().ToString();
var @event = new GetJobReprotDataBlEvent(callerContext, userInput);
WebApiApplication.ApplicationInitializerObj.EventBus.Publish(@event);
XLWorkbook wb = new FileExportEngine().ExportExcel(@event.ReportData); //this is returning XLWorkbook
string fileName = "JobReport_" + DateTime.Now.ToString("yyyy -MM-dd HH':'mm':'ss") + ".xlsx";
using (MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(memoryStream.ToArray())
};
result.Content.Headers.ContentDisposition =
new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}
}
当我从浏览器调用这个 API 时,我可以生成 excel 文件。 http://localhost/ETLScheduler/api/FileExport -- 在浏览器中直接点击时有效
现在我想在 Angular 5 应用程序中使用此 API。我有一个按钮。单击按钮时我调用组件方法 downloadFile() 来下载文件。 下面是代码:
downloadReport() {
this._service.downloadJobReport('AVON');
}
downloadJobReport() 在我的服务文件中,如下所示:
downloadJobReport(clientCode: string) {
return this._http.get(APIs.downloadJobReport);
}
当我运行应用程序并单击下载按钮时,我什么也没得到,我的意思是文件没有下载。谁能知道,我应该如何更新我的角度代码来使用 API。
提前致谢。
【问题讨论】:
-
您是否检查过downloadJobReport方法是否正在执行,是否检查过API方法是否被调用?
标签: c# angular asp.net-web-api closedxml