简体   繁体   English

缓冲到文件流

[英]buffer to file stream

I would like to create file uploading app to box storage. 我想创建文件上传应用程序到存储盒。 I use box-sdk module to upload box. 我使用box-sdk模块上传盒子。 The box-sdk can access a file stream that is came from fs.createReadStream for uploading. box-sdk可以访问来自fs.createReadStream的文件流进行上载。 I use multer module to access uploaded file. 我使用multer模块访问上传的文件。 The multer module has memorystorage to store files. multer模块具有用于存储文件的内存存储。 It produces a buffer string. 它产生一个缓冲字符串。

For uploading box, I have to convert buffer string to file stream. 对于上传框,我必须将缓冲区字符串转换为文件流。 How can I convert it? 如何转换?

You can just create a readable-stream and push your buffer into it. 您可以只创建一个可读流,然后将缓冲区推入其中。

var stream = require('stream')
var bufStream = stream.Readable()
bufStream._read = function () {}
bufStream.push(myBuffer)
bufStream.push(null)
bufStream.pause()

You can then pipe bufStream wherever you need to pipe it. 然后,您可以在需要管道的任何地方通过pipe bufStream

You can also use several , various modules to handle this for you 您还可以使用几个 不同的模块为您处理此问题

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

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