我们如何从文件上传的缓冲区创建读取流,如下所示
const express = require('express');
const router = express.Router();
const multer = require('multer');
const multer = require('zlib');
const fs= require('fs')
router.post('/upload/:recordId', upload.single('file'), async (req, res, next) => {
try {
const buffer=req.file.buffer // this is test.gz file buffer
let write;
var r = fs.createReadStream(buffer);// this line is not working
var z = zlib.createGunzip();
var w = fs.createWriteStream(write); // this line is not working
r.pipe(z).pipe(w);
res.status(204).json({ statusDescription: '204 No Content' });
} catch (ex) {
next(ex);
}
});
这个答案怎么样?请认为这只是几个可能的答案之一。
在此答案中,使用stream
模块。
修改脚本后,请进行如下修改。
var r = fs.createReadStream(buffer);
const stream = require("stream");
var r = new stream.PassThrough().end(buffer);
此外,请进行如下修改。
const multer = require('zlib');
const zlib = require('zlib');
如果我误解了你的问题,而这不是你想要的方向,我深表歉意。