1

I am working on angularjs ,mvc and c#. I need to generate csv file.

From c# code I am returning a Json list as below.

public JsonResult methodname()
{

 return Json(TEST, JsonRequestBehavior.AllowGet);
}

In angular js i am using the following code,

  var req = {
                method: 'POST',
                url: ' ,
                contentType: "application/json" 
            }
            $http(req).success(function (data) {
                if (data) {



                   var dataUrl = 'data:text/csv;utf-9,' + encodeURI(data));
                   var link = document.createElement('a');
                   angular.element(link)
                     .attr('href', dataUrl)
                     .attr('download', "fileName.csv") // Pretty much only works in chrome
                    link.click();

Csv file is downloaded, but data in csv is

[object Object],[object Object]

The actual data passed from c# is as below

0: {AccountNumber: "11910760", AccountName: "1st Central"} 1: {AccountNumber: "11910760", AccountName: "2020 LEGAL LIMITED"}

I need an output like this

11910760,1st Central

11910760,2020 LEGAL LIMITED

Any help is appreciated.

Thanks

JMK
  • 27
  • 1
  • 3
  • Are you sure the JSON given to angular from C# is the one you shown ? If so, it's only angular code to debug. – farbiondriven Nov 15 '17 at 18:30
  • @farbiondriven yes – JMK Nov 15 '17 at 18:31
  • So the JSON shown above is what is housed in the data variable? If so, just look at any of the existing and numerous questions about converting json to csv. https://stackoverflow.com/questions/11257062/converting-json-object-to-csv-format-in-javascript – Trioj Nov 15 '17 at 20:01

0 Answers0