nodejs 向客户端发送文件挂起,直到服务器被杀死

nodejs sending file to client hangs until server is killed

我正在尝试向客户端发送一个 csv 文件。

当我执行此操作时,下载开始,但挂起 "starting...." 并说它正在以 0B/s 传输。

当我杀死服务器时,文件被传输并且都在那里。

这是什么原因造成的?

app.get('/weather_log.csv', function(req, res){
  console.log('sending weather log')
  filePath = 'weather_log.csv'
  var file = fs.readFileSync(filePath, 'binary');
  console.log(typeof(file))
  res.setHeader('Content-Length', fs.statSync(filePath));
  res.setHeader('Content-Type', 'text/csv');
  res.setHeader('Content-Disposition', "attachment;filename=" + filePath);
  res.write(file, 'binary');
  res.end();
})

我的猜测是 res.end() 没有触发,或者客户端没有意识到文件传输由于其他原因未完成。

根据 a0js,答案是使用 res.download(filePath).