从 ASP.NET Web API 2 响应 cross-origin http.get 中获取 Angular 中的特定响应 header(例如 Content-Disposition) ] 要求
Get a specific response header (e.g., Content-Disposition) in Angular from an ASP.NET Web API 2 response for a cross-origin http.get request
当我通过 Angular 服务访问特定的 header (Content-Disposition
) 时,我无法获得它。 CORS 已启用并且 Angular HTTPClient 设置为检索所有 headers.
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));
}
这是 header console.log:
[
"content-type",
"cache-control"
]
我错过了什么?我只想得到 Content-Disposition
header.
在 fileController.cs
文件中,除了设置 Content-Type
和 Content-Disposition
响应 header 之外,您还需要设置 Access-Control-Expose-Headers
:
result.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
请注意,虽然 Fetch 规范实际上允许“*
”作为 Access-Control-Expose-Headers
的值(尽管阅读当前规范文本还不是很清楚……)——浏览器还没有符合规范;因此,您应该明确列出浏览器应向您的前端 JavaScript 代码公开的所有响应 header 名称——除了 Cache-Control
、Content-Language
、Content-Type
、Expires
、Last-Modified
和 Pragma
,它们总是暴露在外。对于除了这六个和您在 Access-Control-Expose-Headers
header 的值中明确列出的响应之外的任何响应 header,浏览器会阻止前端代码访问它们。
httpServletResponse.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
httpServletResponse.setHeader(Content-Disposition,getFileName());
这个解决方案对我有用。
当我通过 Angular 服务访问特定的 header (Content-Disposition
) 时,我无法获得它。 CORS 已启用并且 Angular HTTPClient 设置为检索所有 headers.
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));
}
这是 header console.log:
[
"content-type",
"cache-control"
]
我错过了什么?我只想得到 Content-Disposition
header.
在 fileController.cs
文件中,除了设置 Content-Type
和 Content-Disposition
响应 header 之外,您还需要设置 Access-Control-Expose-Headers
:
result.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
请注意,虽然 Fetch 规范实际上允许“*
”作为 Access-Control-Expose-Headers
的值(尽管阅读当前规范文本还不是很清楚……)——浏览器还没有符合规范;因此,您应该明确列出浏览器应向您的前端 JavaScript 代码公开的所有响应 header 名称——除了 Cache-Control
、Content-Language
、Content-Type
、Expires
、Last-Modified
和 Pragma
,它们总是暴露在外。对于除了这六个和您在 Access-Control-Expose-Headers
header 的值中明确列出的响应之外的任何响应 header,浏览器会阻止前端代码访问它们。
httpServletResponse.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
httpServletResponse.setHeader(Content-Disposition,getFileName());
这个解决方案对我有用。