python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
AngularJs download CSV file JSON data

AngularJs download CSV/PDF file using JSON data

Download CSV file from web API in AngularJs
Example in detail for download CSV file using JSON data.

HTML Code looks like -
    <div class="text-right">
        <a href="javascript:void(0)" class="btn btn-danger" ng-click="downloadCSVFile()">Download CSV</a>
    </div>

AngularJs code looks like -
 $scope.downloadCSVFile = function () {
            var request = {};

            if (typeof (Storage) !== "undefined") {
                request.Role = localStorage.getItem("Key4");
                request.APITokenID = localStorage.getItem("Key2");
                request.InternalIP = localStorage.getItem("Key3");
            }

            var userId = localStorage.getItem("Key5");
            request.PageNo = $scope.PageNo;
            request.PageSize = $scope.PageSize;

            ControllerService.Get("/Account/Check"
                , {}, function (s, r) {
                    if (s) {
                        ControllerService.Post("/User/GetCaseManagement",
                            { obj: request, userID: userId }, function (status, response) {
                                if (status && response.Message == "UNAUTHORIZED") {
                                    //unthorized access
                                    $rootScope.$emit('Logout', {});
                                }
                                else if (response.Status && status) {
                                    //alert(JSON.stringify(response.LoanList));
                                    var row = response.LoanList;
                                    var Head = [[
                                        'Loan App Date',
                                        'Loan App ID',
                                        'Status',
                                        'Loan Amount (£)',
                                        'Term',
                                        'APR (%)',
                                        'Remarks'
                                    ]];

                                    for (var item = 0; item < row.length; ++item) {
                                        Head.push([
                                            row[item].CreatedOn,
                                            row[item].LoanID,
                                            row[item].LoanStatus,
                                            row[item].LoanAmount,
                                            row[item].LoanTerm,
                                            row[item].APR,
                                            row[item].Remark
                                        ]);
                                    }

                                    var csvRows = [];
                                    for (var cell = 0; cell < Head.length; ++cell) {
                                        csvRows.push(Head[cell].join(','));
                                    }

                                    var csvString = csvRows.join("\n");
                                    let csvFile = new Blob([csvString], { type: "text/csv" });
                                    let downloadLink = document.createElement("a");
                                    downloadLink.download = 'CaseManagement.csv';
                                    downloadLink.href = window.URL.createObjectURL(csvFile);
                                    downloadLink.style.display = "none";
                                    document.body.appendChild(downloadLink);
                                    downloadLink.click();
                                }
                                else {
                                    var msg = (response.Message != undefined && response.Message != null) ? response.Message : "something went wrong, while processing your request.";
                                    alert(msg);
                                }
                            });
                    }
                });

        }

Result looks like -
  

ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions