【问题标题】:How to find length of a stream in NodeJS?如何在 NodeJS 中查找流的长度?
【发布时间】:2021-10-08 09:04:24
【问题描述】:

我的代码中有一个函数调用uploadStream(compressedStream),我将compressedStream 作为参数传递给函数,但在此之前我需要确定compressedStream 的长度。 有谁知道我如何在 NodeJS 中做到这一点?

【问题讨论】:

标签: javascript node.js stream node-streams


【解决方案1】:

您可以通过在“数据”事件上获取流块长度来获取长度

compressedStream.on('data', (chunk) => {
   console.log('Got %d characters of string data:', chunk.length);
});

【讨论】:

    【解决方案2】:

    这对我有用:

                let size = 0
                compressedStream.on('data', (chunk) => {
                  size = size + chunk.length
                })
                compressedStream.on('end', () => {
                  console.log(size)
                })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多