一次性读进内存的下载,并发一上来内存就爆,几个 G 的文件能把进程拖死。
按固定大小分块读、边读边写,或者直接用框架自带的流式响应和文件下载方法。
响应头要写对 Content-Length 和 Content-Disposition,文件名含中文记得做编码。
// Node/Express 流式下载
res.setHeader('Content-Disposition',
'attachment; filename="' + encodeURIComponent(name) + '"');
fs.createReadStream(file).pipe(res);
THE END






暂无评论内容