AngularJs图片获取自定义Header

前端之家收集整理的这篇文章主要介绍了AngularJs图片获取自定义Header前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
此处我们主要是为了通过定义自定义指令,处理web页面请求图片时能够通过angular拦截器以便统一携带登陆认证信息
app.directive('httpSrc',[ '$http',function ($http) { var directive = { link: link,restrict: 'A' }; return directive;
function link(scope,element,attrs) {
            var requestConfig = {
                method: 'Get',url: attrs.httpSrc,responseType: 'arraybuffer',cache: 'true'
            };

            $http(requestConfig)
                .success(function(data) {
                    var arr = new Uint8Array(data);

                    var raw = '';
                    var i,j,subArray,chunk = 5000;
                    for (i = 0,j = arr.length; i < j; i += chunk) {
                        subArray = arr.subarray(i,i + chunk);
                        raw += String.fromCharCode.apply(null,subArray);
                    }

                    var b64 = btoa(raw);

                    attrs.$set('src',"data:image/jpeg;base64," + b64);
                });
        }

    }
]);
原文链接:https://www.f2er.com/angularjs/146905.html

猜你在找的Angularjs相关文章