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?