通过Express.js下载动态CSV文件作为附件

时间:2016-09-06 10:05:27

标签: javascript node.js csv express

当GET请求发送到某个API端点时,会动态创建CSV字符串以作为文件附件。

我尝试过以下操作:

const agesMap = 'person1,23\nperson2,32\nperson3,21 ...';
res.setHeader('Content-type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename=ages.csv');
res.write(agesMap);
res.end();

遗憾的是,未触发文件下载,而是仅仅是对原始请求的响应。

请求标题:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Pragma:no-cache

响应标题:

Cache-Control:no-store, no-cache, must-revalidate, proxy-revalidate
Connection:keep-alive
Content-Disposition:attachment; filename=ages.csv
Content-Encoding:gzip
Content-type:text/csv
Pragma:no-cache
Surrogate-Control:no-store
Transfer-Encoding:chunked
Vary:Origin, Accept-Encoding
X-Content-Type-Options:nosniff
X-DNS-Prefetch-Control:off
X-Download-Options:noopen
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block

我做错了什么?

0 个答案:

没有答案