I get issue that I can't get filename from headers of $http response
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
I just want to get filename (Order-414.pdf) as pdf name when downloading. but in this code block:
$http.get(httpPath, { responseType: 'arraybuffer' })
.success(function (data, status, headers) {
debugger;
// just return content-type
var header = headers();
header object just contains content-type.
Object {content-type: "application/pdf"}
I read somewhere that we need config CORS for WebAPI as:
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);
}
But it still doesn't work. Please help me. Thanks in advance.
I suppose you need to add Content-Disposition
instead of filename
into Access-Control-Expose-Headers
cors.ExposedHeaders.Add("Content-Disposition");
Web API: I found that adding the following line code into the ExecuteAsync(...) method of my IHttpActionResult implementation worked ('response' is the HttpResponseMessage to be returned):
response.Content.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With