多个文件追加到节点js的响应对象中

Multiple file append in response object in node js

本文关键字:响应 对象 js 节点 文件 追加      更新时间:2023-09-26
gfs.files.find({}).toArray(function(err, file) {
    if (!err) {
        console.log('file length ', file.length);
        if (file.length > 0) {
            file.forEach(function(each) {
                process.setMaxListeners(0);
                var mime = 'image/jpeg';
                res.set('Content-Type', mime);
                read_stream = gfs.createReadStream({ "filename": each.filename });
                read_stream.pipe(res, { end: false });
                done++;
            });
            read_stream.on('end', function() {
                process.setMaxListeners(0);
                if (done === file.length) {
                    res.send();
                }
            });
        } else {
            res.json('File Not Found');
        }
    }
});

它不能正常工作。浏览器中只显示一个文件。我现在做什么?

如果你的目标客户端是web浏览器,那么你就不能,因为http协议不允许对单个请求进行多次下载。

也许你可以考虑用请求的文件创建一个存档文件(zip)并发送它。