Quoc Nguyen
Quoc Nguyen

Reputation: 360

$http: how to get filename of headers from WebApi with CORS

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.

Upvotes: 5

Views: 4557

Answers (2)

Phil
Phil

Reputation: 1069

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");

Upvotes: 1

Rebornix
Rebornix

Reputation: 5270

I suppose you need to add Content-Disposition instead of filename into Access-Control-Expose-Headers

cors.ExposedHeaders.Add("Content-Disposition");

Upvotes: 9

Related Questions