Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$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.

like image 560
Quoc Nguyen Avatar asked Feb 10 '23 23:02

Quoc Nguyen


2 Answers

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

cors.ExposedHeaders.Add("Content-Disposition");
like image 123
Rebornix Avatar answered Feb 13 '23 13:02

Rebornix


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");
like image 42
Phil Avatar answered Feb 13 '23 11:02

Phil