我在Node.js + Express应用程序中有这段代码,用于在访问/download
路由时向用户提供包含图像的ZIP文件。
res.setHeader('Content-type', 'application/zip');
let fileStream = fs.createReadStream(pathToZip);
fileStream.pipe(res);
fileStream.on('error', function (error) {
/** error handling happening here **/
});
fileStream.on('close', function () {
/** Close Filestream and delete ZIP File from Server **/
fileStream.destroy();
fs.unlink(path);
});
下载文件可在Chrome,Safari甚至IE(最新版本)中使用。尝试使用Firefox(Mac + Windows,FF Quantum 57.0.4)下载时,不会出现下载提示。
Firefox网络检查员向我展示了/download
路由的成功XHR请求,其中包含一长串字符和数字作为响应(响应的字符数:2097152个字符)。
我错过了什么吗?
提前致谢!
答案 0 :(得分:3)
尝试使用attachment
的值设置Content-Disposition标头,以向firefox表明应该下载该文件:
res.setHeader('Content-Disposition', 'attachment');