crypto.decipher导致stream不会在节点js中closures

我试图解密一个文件,并将其发送给客户端的响应。 它可以正常下载文件,如下所示:

input.pipe(res); 

但是当我把解码器joinpipe道时,就像这样:

 input.pipe(decipher).pipe(res); 

它会导致文件下载在浏览器中保持打开状态。 我需要closures解码stream吗? 以下是完整的方法:

 router.get('/', function(req, res, next) { var filePath = 'C:\\Users\\Anthony\\test'; var stat = fs.statSync(filePath); var key = '1234asdf'; var decipher = crypto.createDecipher('aes-256-cbc', key) res.setHeader('Content-Length', stat.size); res.setHeader('Content-disposition', 'attachment; filename=test.mp4'); var input = fs.createReadStream(filePath); input.pipe(decipher).pipe(res); }); 

最有可能发生的事情是您给浏览器编码的文件长度,而不是解密的文件长度,这可能是不同的。 您可以尝试完全省略Content-Length头并查看是否有效(这会导致使用分块编码)。