angular.module('contactsApp') .controller('avatarCtrl', function(ContactService) { var ctrl = this; ctrl.import = ContactService.import.bind(ContactService); ctrl.removePhoto = function() { ctrl.contact.removeProperty('photo', ctrl.contact.getProperty('photo')); ContactService.update(ctrl.contact); $('avatar').removeClass('maximized'); }; ctrl.downloadPhoto = function() { /* globals ArrayBuffer, Uint8Array */ var img = document.getElementById('contact-avatar'); // atob to base64_decode the data-URI var imageSplit = img.src.split(','); // "data:image/png;base64" -> "png" var extension = '.' + imageSplit[0].split(';')[0].split('/')[1]; var imageData = atob(imageSplit[1]); // Use typed arrays to convert the binary data to a Blob var arrayBuffer = new ArrayBuffer(imageData.length); var view = new Uint8Array(arrayBuffer); for (var i=0; i