【发布时间】:2011-09-30 09:09:26
【问题描述】:
因为文件应该是动态生成的,也许我应该使用 fs 模块的 writeStream。但是我无法通过谷歌搜索找到任何示例代码。对不起。
更具体地说,当有人请求时,我想在 MongoDB 中提供包含我的数据的 CSV 文件或 PDF 文件。
任何人,请给我一些提示。
谢谢。
【问题讨论】:
因为文件应该是动态生成的,也许我应该使用 fs 模块的 writeStream。但是我无法通过谷歌搜索找到任何示例代码。对不起。
更具体地说,当有人请求时,我想在 MongoDB 中提供包含我的数据的 CSV 文件或 PDF 文件。
任何人,请给我一些提示。
谢谢。
【问题讨论】:
您不需要 fs,只需在请求处理程序中从 db 流式传输您的数据
您可以使用Content-Disposition header 设置文件元数据
Content-Type: image/jpeg
Content-Disposition: attachment; filename=genome.jpeg;
modification-date="Wed, 12 Feb 1997 16:29:51 -0500";
Content-Description: a complete map of the human genome
【讨论】:
有了express,我可以这样实现。
app.get('/down2', function(req, res){
var filename = 'data.csv';
res.attachment(filename);
res.end('hello,world\nkeesun,hi', 'UTF-8'); //Actually, the data will be loaded form db.
});
它是多么简单。谢谢。
【讨论】:
res.send(generatedCSVcontent)?
res.end 不应该用于较大的文件,否则它将始终下载 0 字节的文件