无法弄清楚在哪里设置{responseType:'arraybuffer'}

时间:2017-09-08 16:50:21

标签: angularjs

我一直在尝试在以下代码中将responseType设置为'arrayBuffer',但我不断获得Failed to load pdf document

var resource =  ApiService.getResource('/cases/report');

   resource.get({caseId: self.CaseID,responseType:'arraybuffer'},function(data){
    var file = new Blob([data], {type: 'application/pdf'});
    var fileURL = URL.createObjectURL(file);
    window.open(fileURL);
   }); 

我尝试在getResource方法中添加{responseType: 'arraybuffer'}以及get方法中的其他各个位置,但我收到错误Failed to Load Pdf Document

我也试过在这里添加它:

var resource = $resource('url', {},{responseType: 'arraybuffer'});

但这不起作用

完整代码:

 function getResource(endpoint) {
        var resource = $resource(endpoint, null);
        return wrapActions(resource);
    }

  function wrapActions(resource) {
        var wrappedResource = resource;
        for (var i = 0; i < self.actions.length; i++) {
            self.wrapAction(wrappedResource, self.actions[i]);
        }

        // return modified copy of resource
        return wrappedResource;
    }


    function wrapAction(resource, action) {
        // copy original action
        resource['_' + action] = resource[action];

        // create new action wrapping the original and sending token
        resource[action] = function (data, success, error) {
            data = data || {};

            return resource['_' + action](
                angular.extend({}, data),
                function (response) {
                    handleSuccess(response, success);
                },
                function (response) {
                    handleError(response, error);
                }
            );
        };
    }

    function handleSuccess(response, callback) {
        if (callback) {
            callback(response);
        }
    }

    function handleError(response, callback) {
        if (typeof response.status !== 'undefined') {
            switch (response.status) {
                case '403' :
                case 403 :
                case '401' :
                case 401 :
                    handle401();
                    return;
            }
        }

        if (callback) {
            callback(response);
        }
    }

    function handle401() {
        if ($state.current.name != 'login') {
            AuthService.setRedirect($state.current.name, $stateParams);
        }

        AuthService.logout();
        $state.go('login');
    }
}

0 个答案:

没有答案