How to Use file saver in angular

Posted By : Murari Kumar | 28-Dec-2016

Frequently, we have need to save file as a pdf  or txt or sls. For save the file we can use file saver in angular.

If I want to use file saver in angular then we need to follow following step.

Step-1:

Install angular file saver using following command.

#bower install angular-file-saver.

Step-2

Include ngFileSaver module into your project.

Following code to save file as pdf.

 


 
 function orderPdf(reportId, reportType, orderId){
          $http.get('/skulocity/api/v1/crmsfa/orders/pdf?orderId='+orderId+'&reportId='+reportId+'&reportType='+reportType, {responseType: 'arraybuffer'})
           .success(function (data) {
             var file = new Blob([data], {type: reportType});
              var fileName = "";
              if(fileName.indexOf("pdf")>-1){
                  fileName = "order_"+orderId+".pdf";
              }else if(fileName.indexOf("csv")>-1){
                  fileName = "order_"+orderId+".csv";
              }
                FileSaver.saveAs(file, fileName);
           });
        }
 

About Author

Author Image
Murari Kumar

Murari is a bright Web developer. He has expertise in Java.

Request for Proposal

Name is required

Comment is required

Sending message..