Friday 14 April 2017

Download pdf file on client side

I am a beginner for MEAN stack. I want to download and display a pdf file to the client. For that my server side script as under:

app.get("/pdf", function(req, res)
    {
        var file = __dirname + '/try.pdf';
        res.download(file, function (err) 
            {
               if (err) {
                   console.log("Error " + err);
               } else {
                   console.log("Success");  // server shows success but file is not downloaded to client 
               }
               res.end();
            }
        );
    }
);

After starting server when I give command localhost/pdf in browser the file is downloaded. But, when I connect to server through index page and attach this module to ng-click event of a button, server shows success but the file is not downloaded to client. My angularJS code is as under which also displays both success and then clause message:

        app.controller('showPDF', function($scope, $http)
            {
                $scope.pdfShow = function()
                {
                    alert("Inside pdfShow");

                    $http.get("/pdf" ).
                    success(function(response){alert("PDF Success")}).
                    error(function(error, status){alert("PDF Error " + error + " status " + status); }).
                    then(function(response){alert("PDF then clause ")})
                } 
            }
        );


        <button ng-controller="showPDF" ng-click="pdfShow()">Show PDF file </button>

Is there any mistake I am doing? Please guide me.

Thanks.



via Nizamuddin Shaikh

No comments:

Post a Comment