微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决'content-disposition'标题在浏览器中显示为空的问题

如何解决如何解决'content-disposition'标题在浏览器中显示为空的问题

在我的 Angular 应用程序中,我使用了从 Web API 核心返回的 excel 文件。我还在 API 方法的 content-disposition 标头中设置了一个文件名。但是我无法从 API 获取 content-disposition 标头。如何解决这个问题。

api方法代码为:

    [HttpGet("ExportToExcelAsync")]
    public async Task<IActionResult> ExportToExcelAsync(int id,int rId)
    {  

            var Details = await _Service.GetDetails(id,rId);                
            var excelFile = exportToExcel.ExportExcel(Details,templatePath).Result;               
            var content = excelFile.ToArray();
            var actualFile = Details.DocumentName;
            Response.Headers.Add("x-file-name","" + actualFile + ".xlsx");
            Response.Headers.Add("Content-disposition","" + actualFile + ".xlsx");
                          
            return File(
           content,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","" + actualFile + ".xlsx");          
    } 

在我的角边代码中:

exportToExcel(){

this.Service.ExportToExcel(Id,rId).subscribe((response) => {
var contentdisposition=response.headers.get('content-disposition');
this.file= contentdisposition.split(';')[1].trim().split('=')[1]; 
this.downloadFile(response,this.file);

}); }

解决方法

您可以在 ASP.NET Core WebAPI 后端应用程序中启用 CORS 时尝试 specify the headers which need to be exposed to the client,如下所示。

.WithExposedHeaders("x-file-name","Content-Disposition")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。