Using Angular.js - how to serve binary data from a backend requiring authentication?

In my angularjs application, I am communicating with a server server that requires basic authentication of access through an HTTP header. I implemented a client-side authentication mechanism as described here .

angular.module('myAuthModule')
.config(['$httpProvider', '$stateProvider',
    function ($httpProvider, $stateProvider) {
        $httpProvider.interceptors.push('securityInterceptor');
    }])
.factory('securityInterceptor', ['$location', '$window', '$q',
    function ($location, $window, $q) {
        return {
            request: function (config) {
                config.headers = config.headers || {};
                if ($window.sessionStorage.token) {
                    config.headers['Auth-Key'] = $window.sessionStorage.token;
                }
                return config;
            },
            response: function (response) {
                if (response.status === 401 || response.status === 403) {
                    $location.path('/login');
                }
                return response || $q.when(response);
            }
        };
    }
]);

So far, it's so good that handling xhr requests in an angular application works as expected.

, PDF-. /Document/Pdf/:id, application/pdf ContentDisposition: attachment, . , xhr, ngHref , , , $window.open('/Document/Pdf/13'), 401 Unauthorized .

?

+3
2

, @Geoff Genz, data-uri, , , - .

API, . angular

.factory('fileFactory', ['$http', '$window',
    function ($http, $window) {
        return {
            downloadFile: function (fileId) {
                return $http(
                    {
                        method: "POST",
                        data: fileId,
                        url: '/api/Files/RequestDownloadLink',
                        cache: false
                    }).success(function (response) {
                        var url = '/api/File/' + response.downloadId;
                        $window.location = url;
                    });
            }
        };
    }]);

, -. , - .

+5

. , Ajax, . GET, (, href) POST (, ). , :

(1) Basic Digest auth -, .

(2) cookie "", .

(3) , , , - POST GET . POST IFrame Content-Disposition, "attachment; filename =" blah.pdf "" . .

, , , .

+1

All Articles