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

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. Commented Nov 15, 2017 at 18:30
  • @farbiondriven yes Commented Nov 15, 2017 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. stackoverflow.com/questions/11257062/… Commented Nov 15, 2017 at 20:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.