angular - 从核心 API 返回 Excel 文件
问题描述
我有 dotnet Core API,它返回 excel 文件(使用 ClosedXML)数据并以角度 API 方法使用此 API
using (MemoryStream stream = new MemoryStream())
{
var workbook = new XLWorkbook();
var SheetNames = new List<string>() { "15-16", "16-17", "17-18", "18-19", "19-20" };
foreach (var sheetname in SheetNames)
{
var worksheet = workbook.Worksheets.Add(sheetname);
worksheet.Cell("A1").Value = sheetname;
}
workbook.SaveAs(stream);
stream.Seek(0, SeekOrigin.Begin);
return this.File(
fileContents: stream.ToArray(),
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// By setting a file download name the framework will
// automatically add the attachment Content-Disposition header
fileDownloadName: "ERSheet.xlsx"
);
}
以角度传递 API 请求的标头
'Content-Type': 'aapplication/vnd.ms-excel',
'Authorization': 'Bearer ' + token
这是我从 API 获得角度响应后的代码
download(data: Blob) {
const contentType = 'application/vnd.openxmlformats-ficedocument.spreadsheetml.sheet';
const blob = new Blob([data], { type: contentType });
const url = window.URL.createObjectURL(blob);
window.open(url);
}
但这不起作用。任何想法如何从 API 返回 excel 文件数据并通过单击按钮在角度侧下载。
解决方案
推荐阅读
- c - C:如何过滤正在读入管道的值
- asp.net - asp.net - 具有关系数据的 LINQ 查询
- android - Android Exoplayer:“onPlayerStateChanged(boolean playWhenReady, int playbackState)”方法中的“playWhenReady”是什么
- json - 从网站获取 JSON 时出现错误
- r - R:在所有父环境中绑定的替代变量
- google-analytics - 从 Google Analytics 中消除机器人流量
- c# - 如何处理 MVVM 模式中嵌套 ListView 的 ItemClick?
- docker - IBM 云:无法构建应用程序
- node.js - 在 Google Cloud 中使用 Node.js API 将 IP 附加到 VM 实例
- angular - angular - 安全地获取延迟加载的 DOM 元素的引用