【发布时间】:2018-05-18 21:54:18
【问题描述】:
当我通过 Angular 服务访问特定标头 (Content-Disposition) 时,我无法获得它。 CORS 已启用,并且 Angular HTTPClient 设置为检索所有标头。
Startup.cs
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
ConfigureOAuth(app, config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
fileController.cs
[Route("file/{idFile}")]
public HttpResponseMessage GetFile(string idFile)
{
string file;
byte[] file;
document = fileService.GetFile(idDFile, out fileName);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(file)
};
result.Content.Headers.ContentDisposition =
new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = nomeFile
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}
fileService.ts
getFile(fileId: number): Observable<Response> {
const url = `${urlBackEnd}/file/${fileId}`;
return this.http.get(url, { observe: 'response', responseType: 'blob' })
.map(res => {;
console.log(res.headers.keys()); // There's no CONTENT-DISPOSITION
saveAs(<Blob>res.body);
return res.body;
})
.catch(e => this.handleErrors(e));
}
这里是标头 console.log:
[
"content-type",
"cache-control"
]
我错过了什么?我只想获取Content-Disposition 标头。
【问题讨论】:
-
您发送跨域请求的服务器必须在响应中包含 na Access-Control-Expose-Headers 标头,并在其值中包含 Content-Disposition。在stackoverflow.com/questions/39020385/…查看答案
-
感谢@sideshowbarker,我尝试这样做,但仍然无法获得该标题。 i.imgur.com/NemwLRb.png
result.Content.Headers.Add("Access-Control-Expose-Headers", "*"); -
我认为浏览器还不支持 Access-Control-Expose-Headers 的
*通配符值。尝试在值中明确列出 Content-Disposition。
标签: c# angular cors asp.net-web-api2 owin