【发布时间】:2019-03-19 19:57:17
【问题描述】:
我在 Asp Net Core 中遇到了问题。
我不能将文件作为 blob 数据返回,但不能作为带有编码内容的 JSON 对象返回到 base64。
这是我的 C# 代码:
[Route("DownloadExcel")]
[HttpPost]
public async Task<FileContentResult> DownloadExcel([FromBody] Queries.In.Pages.Users.ExportToExcel.InModel model)
{
//some logic to retrieve an array of bytes.
var filename = $"myFile_{DateTime.Now:yyyyMMdd_hh_mm}.xlsx";
return File(byteArray, "octet/stream", filename);
}
在 Angular 6 侧:
this.httpClient.post(url, model, {headers: headers, responseType: 'blob'} )
.subscribe(res => {
let blob = new Blob(["\ufeff", response], {type: 'octet/stream'});
saveAs(blob, '111.xlsx');
})
我的项目有什么问题以及如何修改它以检索 blob 而不是 JSON?
提前致谢!
【问题讨论】:
-
您为什么使用
octet/streamMIME 类型而不是正确的excel 类型application/vnd.openxmlformats-officedocument.spreadsheetml.sheet? -
请试试这个stackoverflow.com/questions/41984491/…改变逻辑accodingly
-
我觉得应该是IActionResult返回类型的Task。
标签: c# angular asp.net-core blob angular6