node.js导出json,csv格式
发布于 12 年前 作者 banther 13654 次浏览 最后一次编辑是 8 年前

var nation_export_save = function(req, res) { var encode = req.body.encode;(encode为‘utf8’ or ‘GBK’) var sname = req.body.sname; var items = req.body.items; Nation.find({}, function(err, nations) { if (sname == ‘JSON’) { res.attachment(‘nation.txt’); res.send(nations); } else if (sname == ‘CSV’) { var ites = []; var lines = items.split(’,’); for (var i = 0; i < lines.length; i++) { ites.push(lines[i]); }; json2csv({ data: nations, fields: ites }, function(err, csv) { var iconv = new Iconv(‘UTF-8’, encode); content = iconv.convert(csv); res.attachment(‘nation.csv’); res.send(content); })

    };
})

}

回到顶部