domingo, 3 de mayo de 2015

Angularjs - Descargar archivos del servidor

Para descargar un documento pdf que se encuentra en un servidor y se enlaza a través de una base de datos utilizando #angularjs, el procedimiento seguido por mi fue:
En la plantilla o página agregamos un enlace para descargar el documento

<!--- template or index.html -->

<a data-click-prevent="true" href="{{data.link}}">{{data.title}}</a>
o
<a data-click-prevent="true" href="#/document/123">{{data.title}}</a>

Crear un servicio tal como

<!-- create factory -->
Inyectamos $http

app.factory("services", ['$http', function($http) {
  var serviceBase = 'services/';
    var obj = {};
    obj.getDocument = function(numberID){
//        http://stackoverflow.com/questions/25781927/how-to-read-pdf-stream-in-angularjs
        <!-- document It belongs to our api -->
        return $http.get(serviceBase + 'document?id=' + numberID, {responseType: 'arraybuffer'})
           .success(function (data) {
               var file = new Blob([data], {type: 'application/pdf'});
               var fileURL = URL.createObjectURL(file);
               window.open(fileURL);
        });
    };
    return obj;
}]);

Luego creamos una directiva que evite que el enlace se redirija a la dirección de la api que controla nuestra aplicación; inyectando la factory creada con nombre "services"

    app.directive('clickPrevent', function(services) {
      return function(scope, element, attrs) {
        return element.on('click', function(e) {
          // attrs es el objeto que recoge los datos del enlace pulsado
          var doc = attrs.href.split('/')[2];
          // doc recoge el numero del documento pej 123 y lo pasa al servicio
          services.getDocument(doc);
          return e.preventDefault();
        });
      };
    });

mi api quedaria como

<?php

class api {

        private $db = NULL;
        private $mysqli = NULL;

        public function __construct(){
            parent::__construct();                // Init parent contructor
            $this->dbConnect();                    // Initiate Database connection
        }

        /**
         * [dbConnect Connect to Database]
         * @return [type] [description]
         */
        private function dbConnect(){
            $this->mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB);
        }

        /*
         * Dynmically call the method based on the query string
         */
        public function processApi(){
            $func = strtolower(trim(str_replace("/","",$_REQUEST['x'])));
            if((int)method_exists($this,$func) > 0)
                $this->$func();
            else
                $this->response('',404); // If the method not exist with in this class "Page not found".
        }


        function document(){
            $id = (int)$this->_request['id'];
            $sql = "SELECT files_id,link,title FROM `files` WHERE `files_id`= ?";
            //obtengo el link de descarga para proceder
            $sentencia = $this->mysqli->stmt_init();
            $sentencia->prepare($sql);
            $sentencia->bind_param('i', $id);
            $sentencia->execute();
            $sentencia->bind_result($files_id,$link,$title);
            $sentencia-> fetch();
            $root = "../releases/";
            $ruta = realpath($root.$link);
            $type = '';
            if( file_exists($ruta)){
                $size = filesize($ruta);
                if (function_exists('finfo_file')) {
                    $info = finfo_open(FILEINFO_MIME);
                    $type = finfo_file($info, $ruta);
                    finfo_close($info);
                }
                if ($type == '') {
                    $type = "application/force-download";
                }
                // Define headers
                header("Content-Type: $type");
                header("Content-Disposition: attachment; filename=".$title.".pdf");
                header("Content-Transfer-Encoding: binary");
                header("Content-Length: " . $size);
                // Download file
                readfile($ruta);
            }else {
                echo "document does not exist";
            }
        }

}
Eso es todo,  cambio y fuera.

No hay comentarios:

Publicar un comentario