$ http:如何使用CORS从WebApi获取头文件名

Quo*_*yen 4 cors asp.net-web-api angularjs

我遇到问题,我无法从$ http响应的标头中获取文件名

HTTP/1.1 200 OK
Content-Length:121257
Content-Type:application/pdf
服务器:Microsoft-HTTPAPI/2.0
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:*
Content-Disposition:attachment; filename = Order-414.pdf
日期:星期三,2015年2月11日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();
Run Code Online (Sandbox Code Playgroud)

header对象只包含content-type.

Object {content-type: "application/pdf"}
Run Code Online (Sandbox Code Playgroud)

我在某处读到我们需要为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);
    }
Run Code Online (Sandbox Code Playgroud)

但它仍然无效.请帮我.提前致谢.

Reb*_*nix 8

我想你需要加入Content-Disposition而不是filename加入Access-Control-Expose-Headers

cors.ExposedHeaders.Add("Content-Disposition");
Run Code Online (Sandbox Code Playgroud)