Source: directives/botao-anexo-imagem/botao-anexo-imagem.js

/**
 * @ngdoc directives
 * @name botao-anexo-imagem
 * @module s4c.directives.botaoLeituraArquivo.botaoAnexoImagem
 * 
 * @description `botaoAnexo` Inclui a opção de anexar arquivo
 *              
 *              
 * 
 * @example <botao-anexo></botao-anexo>
 *
 */
(function () {
    'use strict';

    function botaoAnexo() {
        return {
            scope: {
                image: '=image'
            },
            templateUrl: 'app/directives/botao-anexo-imagem/botao-anexo-imagem.html',
            link: function ($scope, $elem) {
                $scope.res = $scope.$root.res;
                $scope.image = {};

		       /**
		        *
		        * Abre a janela para anexar o arquivo 
		        *
		        * @method abrirUpload
				*        
		        *
		        */   
                $scope.abrirUpload = function () {
                    var input = $elem.find('input[type="file"]');
                    input.click();
                };

		       /**
		        * Faz o envio dos anexos para o servidor 
		        *
		        * @method uploadImage
				*        
		        * @param ctx {Object} Arquivos anexados
				*               
		        */   
                $scope.uploadImage = function (ctx) {

                    var reader = new FileReader();
                    reader.onloadend = function (file) {
                        // File as base64
                        $scope.image.data = reader.result;
                        $scope.$apply();
                    };

                    $scope.image.name = ctx.files[0].name;

                    reader.readAsDataURL(ctx.files[0]);
                };

            }
        };
    }

    angular.module('s4c.directives.botaoAnexo', [])
        .directive('botaoAnexo', botaoAnexo);
}());