我正在尝试下载csv文件。我找到了一些教程,我跟随这个例子:
var json2csv = require('json2csv');
router.get('/api/exportmetric', function(req, res) {
console.log('export metric hit');
var fields = ['first_name', 'last_name', 'age'];
var people = [
{
"first_name": "George",
"last_name": "Lopez",
"age": "31"
}, {
"first_name": "John",
"last_name": "Doe",
"age": "15"
}, {
"first_name": "Jenna",
"last_name": "Grassley",
"age": "44"
}
];
json2csv({ data: people, fields: fields }, function(err, csv) {
res.setHeader('Content-disposition', 'attachment; filename=testing.csv');
res.set('Content-Type', 'text/csv');
console.log(csv)
res.status(200).send(csv);
});
});
如果我检查我的网络控制台选项卡,我会获得200
状态;但是,该文件未显示在我的downloads
文件夹中。
有人可以帮忙吗?
谢谢!