Javascript createReadStream().pipe() 回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14962085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:27:37  来源:igfitidea点击:

createReadStream().pipe() Callback

javascriptnode.js

提问by AlbertEngelB

Sorry in advance, I have a couple of questions on createReadStream() here.

提前抱歉,我在这里有几个关于 createReadStream() 的问题。

Basically what I'm doing is dynamically building a file and streaming it to the user using fs once it is finished. I'm using .pipe() to make sure I'm throttling correctly (stop reading if buffer's full, start again once it's not, etc.) Here's a sample of my code I have so far.

基本上我正在做的是动态构建文件并在完成后使用 fs 将其流式传输给用户。我正在使用 .pipe() 来确保我正确节流(如果缓冲区已满则停止读取,一旦未满就重新开始,等等)这是我迄今为止的代码示例。

http.createServer(function(req, res) {
  var stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024})

  stream.pipe(res);

}).listen(3002, function() {
  console.log('Server listening on port 3002')
})

I've read in another StackOverflow question (sorry, lost it) that if you're using the regular res.send() and res.end() that .pipe() works great, as it calls the .send and .end and adds throttling.

我在另一个 StackOverflow 问题(抱歉,丢失了)中读到,如果您使用的是常规 res.send() 和 res.end(),那么 .pipe() 效果很好,因为它调用了 .send 和 .end并添加节流。

That works fine for most cases, except I'm wanting to remove the file once the stream is complete and not using .pipe() means I'm going to have to handle throttling myself just to get a callback.

这在大多数情况下都可以正常工作,除非我想在流完成后删除文件而不使用 .pipe() 意味着我将不得不自己处理节流以获得回调。

So I'm guessing that I'll want to create my own fake "res" object that has a .send() and .end() method that does what the res usually does, however on the .end() I'll put additional code to clean up the generated file. My question is basically how would I pull that off?

所以我猜我想创建我自己的假“res”对象,它有一个 .send() 和 .end() 方法来做 res 通常做的事情,但是在 .end() 上我会放置额外的代码来清理生成的文件。我的问题基本上是我将如何实现它?

Help with this would be much appreciated, thanks!

对此的帮助将不胜感激,谢谢!

回答by loganfsmyth

The first part about downloading can be answered by Download file from NodeJS Server.

关于下载的第一部分可以通过从 NodeJS 服务器下载文件来回答。

As for removing the file after it has all been sent, you can just add your own event handler to remove the file once everything has been sent.

至于在发送完所有文件后删除文件,您只需添加自己的事件处理程序即可在发送完所有文件后删除文件。

var stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024})
stream.pipe(res);

var had_error = false;
stream.on('error', function(err){
  had_error = true;
});
stream.on('close', function(){
  if (!had_error) fs.unlink('<filepath>/example.pdf');
});

The errorhandler isn't 100% needed, but then you don't delete the file if there was an error while you were trying to send it.

error处理器是不是100%必要的,但这时如果有一个错误,而你正在尝试发送它不会删除该文件。