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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^