我在 expressjs (nodejs) 应用程序中将数据从服务器流式传输到客户端。
我做了这样的事情;
// let contentType and filename be correctly defined in context
function(req,res){
res.setHeader('Content-Type',contentType);
res.setHeader('Content-Disposition','attachment; filename='+filename);
require('fs').createReadStream('/path/file').pipe(res);
}
我想知道客户端何时无法下载文件,但我不知道如何继续。
最佳答案
监听响应对象上的 close
事件。当与客户端的连接过早关闭(如刷新页面或关闭选项卡)时会发出此消息。
require('fs').createReadStream('/path/file').pipe(res).on('close',function(){
//do stuff for closed connection
})
关于javascript - 如何在 Node 中通过 http 管道流检测客户端关闭连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586763/