angular8 - 无法从 Angular 8 下载 excel 文件,服务器端代码是 .net core webapi 3.1
问题描述
抱歉重复的问题,但现有的解决方案都没有解决我的问题。
尝试从 Angular8 下载 excel 文件时,出现以下错误。
我使用.net core web api 3.1实现了返回文件的方法。下面是返回excel文件的方法。
[Route("downloadClaims")]
[HttpGet]
public FileResult DownloadClaims(int userId)
{
var memory = new MemoryStream();
using (var stream = new FileStream("E:/AddClaim.xlsx", FileMode.Open))
{
stream.CopyTo(memory);
}
memory.Position = 0;
var mimeType = "application/vnd.ms-excel";
return File(memory, mimeType, "AddClaim.xlsx");
}
处理Angular8中的下载功能。请在下面找到我的角度服务代码。
downLoadAllClaims():Observable<Blob>
{
let httpOption = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'responseType': 'blob' as 'json',
'Access-Control-Expose-Headers':'Content-Disposition'
, 'observe': 'response'
})
}
return this.http.get<Blob>(baseURL+'/downloadClaims',httpOption);
}
我的组件代码如下。
downLoadClaim(_evnt):void{
this.claimService.downLoadAllClaims().subscribe(
blob => {
saveAs(blob,"AllClaims.xlsx")
},
error => {
console.log("Something went wrong while downloading");
});
}
文件没有下载。不明白我错过了什么?
解决方案
推荐阅读
- ios - 如果我选择低于 11.0.0 的 IOS 部署目标,则构建失败
- flutter - 如何将文本字段控制器传递到按钮选择器颤动小部件
- python - 如何从所有页面获取数据?
- javascript - Javascript变量替换(CK编辑器)
- java - 下载图像,但在 kotlin 中图像的大小不正确
- python - 如何使用 Python 显示来自 Oracle 数据库的 2 字节字母?
- node.js - 在 ES5 中扩展 Node 的 EventEmitter
- kubernetes - 如何将相同的卷挂载到 kubernetes 命名空间中的所有 pod
- android - 如何在我的项目中从库中覆盖 xml 视图(使用数据绑定)(# Override_Views_Data_Binding_Android)
- javascript - 分配变量后,脚本停止工作