简体   繁体   English

我可以从 fs.createWriteStream() 获取缓冲区吗?

[英]Can I get the Buffer from fs.createWriteStream()?

I have simple question - Can I get the Buffer from fs.createWriteStream()?我有一个简单的问题 -我可以从 fs.createWriteStream() 获取缓冲区吗?

In my case I use archiver NPM module which may create archives from Buffer.在我的情况下,我使用归档器NPM 模块,它可以从缓冲区创建档案。 When this module end work with method archive.finalize() I may get result from archive.pipe(stream) .当这个模块使用方法archive.finalize()结束工作时,我可能会从archive.pipe(stream)得到结果。 I want have Buffer from fs.createWriteStream() which I may send to res.send() Express method.我想要来自fs.createWriteStream()缓冲区,我可以将其发送到res.send() Express 方法。

let archive = archiver("zip", {
      zlib: { level: 9 } // Sets the compression level.
    });
const stream = fs.createWriteStream(
      "./src/backend/api/test.zip"
    );
archive.pipe(stream);
archive.append(buffer, { name: "test.csv" });
archive.finalize();

If I understood your problem correctly, that might help you如果我正确理解了您的问题,那可能会对您有所帮助

 const stream = fs.createWriteStream( "./src/backend/api/test.zip" ); stream.on('data', (chunk) => { //here u get every chunk as a buffer }) stream.on('finish', () => { //here your code if the stream is ending })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM