directive.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. starter.directive('httpSrc', function ($http, global) {
  2. return {
  3. restrict: "A",
  4. link: function (scope, element, attrs) {
  5. var requestConfig = {
  6. method: 'GET',
  7. responseType: 'arraybuffer',
  8. cache: 'true'
  9. };
  10. function base64Img(data) {
  11. var arr = new Uint8Array(data);
  12. var raw = '';
  13. var i, j, subArray, chunk = 5000;
  14. for (i = 0, j = arr.length; i < j; i += chunk) {
  15. subArray = arr.subarray(i, i + chunk);
  16. raw += String.fromCharCode.apply(null, subArray);
  17. }
  18. return btoa(raw);
  19. };
  20. attrs.$observe('httpSrc', function (newValue) {
  21. requestConfig.url = newValue;
  22. global.fetch_user().then(function () {
  23. $http(requestConfig).then(function (data) {
  24. attrs.$set('src', "data:image/jpeg;base64," + base64Img(data.data));
  25. });
  26. });
  27. });
  28. }
  29. }
  30. })