0

I want to export the data collection in csv format in MEAN stack.

This is my csv.server.controller function:

function(err, source) {
        var result = [];
        //res.setHeader('content-type', 'text/csv');

        res.setHeader('content-disposition', 'attachment; filename=user.csv');

        csv()
            .from(source,{delimiter:'|'})
            .on('data', function(data){
                result.push(data);
            })
            .on('end', function(){
                res.send(result);
            });
    });

In the angularjs controller I am calling :

$scope.csv = function() {
                    $http.get('/download'). success(function(data, status, headers,config) {
                        var element = angular.element('<a/>');

                        element.attr({
                            href: 'data:text/csv;charset=utf-8,' + encodeURI(data),
                            target: '_blank',
                            download: 'users.csv'
                       })[0].click();

                    }).
                        error(function(data, status, headers, config) {
                            console.log(data);
                        });
                };
            });

which is routed to :

app.route('/download').get(csv.download);

This whole structure works fine for around 700 rows/collections. But foe more than that I get a browser error for Out Of Memory.

Can anyone please help me understand the issue. I am thinking that I am writing the source file all at once to csv form. How can i do it in chunks to avoid the memory exception?

1 Answer 1

0

Use this for CSV

Step 1: npm install csv

var csv = require('csv');

csv.generate({seed: 1, columns: 2, length: 20}, function(err, data){
csv.parse(data, function(err, data){
csv.transform(data, function(data){
  return data.map(function(value){return value.toUpperCase()});
}, function(err, data){
  csv.stringify(data, function(err, data){
    process.stdout.write(data);
           });
       });
   });  
});
2
  • So I need to change the CSV version ? Is it not possible to do using the same version
    – user927026
    Jan 13, 2015 at 11:22
  • The simple way to deal with this problem: form array similar to:- [[a,1],[b,2],[c,3],[d,4],[e,5]] Use aggression Jan 13, 2015 at 17:03

Your Answer

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