$http:如何使用 CORS 从 WebApi 获取 headers 的文件名
$http: how to get filename of headers from WebApi with CORS
我遇到无法从 header 的 $http 响应
中获取文件名的问题
HTTP/1.1 200 OK
Content-Length: 121257
Content-Type: application/pdf
Server: Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: *
Content-Disposition: attachment; filename=Order-414.pdf
Date: Wed, 11 Feb 2015 05:32:25 GMT
我只想在下载时获取文件名 (Order-414.pdf) 作为 pdf 名称。但在此代码块中:
$http.get(httpPath, { responseType: 'arraybuffer' })
.success(function (data, status, headers) {
debugger;
// just return content-type
var header = headers();
header object 只包含 content-type.
Object {content-type: "application/pdf"}
我在某处读到我们需要为 WebAPI 配置 CORS:
private static void RegisterCorsConfig(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*", "*");
//var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion");
//cors.ExposedHeaders.Add("*");
//cors.ExposedHeaders.Add("filename");
config.EnableCors(cors);
}
但是还是不行。请帮我。
提前致谢。
我想您需要将 Content-Disposition
而不是 filename
添加到 Access-Control-Expose-Headers
cors.ExposedHeaders.Add("Content-Disposition");
Web API:我发现将以下行代码添加到我的 IHttpActionResult 实现的 ExecuteAsync(...) 方法中有效 ('response'是要返回的 HttpResponseMessage):
response.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
我遇到无法从 header 的 $http 响应
中获取文件名的问题HTTP/1.1 200 OK
Content-Length: 121257
Content-Type: application/pdf
Server: Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: *
Content-Disposition: attachment; filename=Order-414.pdf
Date: Wed, 11 Feb 2015 05:32:25 GMT
我只想在下载时获取文件名 (Order-414.pdf) 作为 pdf 名称。但在此代码块中:
$http.get(httpPath, { responseType: 'arraybuffer' })
.success(function (data, status, headers) {
debugger;
// just return content-type
var header = headers();
header object 只包含 content-type.
Object {content-type: "application/pdf"}
我在某处读到我们需要为 WebAPI 配置 CORS:
private static void RegisterCorsConfig(HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*", "*");
//var cors = new EnableCorsAttribute("*", "*", "*", "DataServiceVersion, MaxDataServiceVersion");
//cors.ExposedHeaders.Add("*");
//cors.ExposedHeaders.Add("filename");
config.EnableCors(cors);
}
但是还是不行。请帮我。 提前致谢。
我想您需要将 Content-Disposition
而不是 filename
添加到 Access-Control-Expose-Headers
cors.ExposedHeaders.Add("Content-Disposition");
Web API:我发现将以下行代码添加到我的 IHttpActionResult 实现的 ExecuteAsync(...) 方法中有效 ('response'是要返回的 HttpResponseMessage):
response.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");