アクセス時に特定のヘッダー(Content-Disposition
)を取得できませんAngularサービスを介して。 CORSが有効になり、Angular HTTPClientがすべてのヘッダーを取得するように設定されます。クロスオリジンhttp.get要求のASP.NET Web API 2応答から特定の応答ヘッダー(Content-Dispositionなど)を取得
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
ヘッダーを取得したいです。あなたのfileController.cs
ファイルで
送信元からの要求を送信するサーバーには、Content-Dispositionをその値に持つレスポンスにna Access-Control-Expose-Headersヘッダーが含まれている必要があります。回答を見るhttps://stackoverflow.com/questions/39020385/fetch-api-not-returning-the-location-header/39021128#39021128 – sideshowbarker
ありがとう@sideshowbarker、私はそれをやってみたが、まだ、私はそのヘッダーを取得することはできません。 https://i.imgur.com/NemwLRb.png 'result.Content.Headers.Add(" Access-Control-Expose-Headers "、" * ");' – Dyd666
私はまだブラウザが ' 'Access-Control-Expose-Headersのワイルドカード値。代わりに、値にContent-Dispositionを明示的にリストしてください。 – sideshowbarker