Multer - chunk argument not valid

263 views Asked by At

I have a problem with stream.pipeline()

await pipeline(
    req.file.buffer,
    fs.createWriteStream(
        `${__dirname}/../client/public/profil/${fileName}`
    )
)

I have this error

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type number (255)

req.file :

{
  fieldname: 'file',
  originalname: 'test.jpeg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e1 00 16 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 00 00 00 00 00 ff db 00 43 00 05 03 04 04 04 03 05 04 04 04 05 05 05 06 07 0c 08 07 07 ... 195029 more bytes>,
  size: 195079
}

I try to use Readable

const stream = require("stream")
const Readable = stream.Readable;
const ReadableChunk = new Readable({
  read(size) {
    if (this.data.length) {
        const chunk = this.data.slice(0, size);
        this.data = this.data.slice(size, this.data.length);               
        this.push(chunk);        
    } else {
        this.push(null); // 'end', no more data
    }
}});

module.exports.test= async (req, res) => { const filename = 'test.jpg' await pipeline( ReadableChunk(req.file.buffer), fs.createWriteStream(${__dirname}/../client/public/profil/${fileName}) ); }

but now i have this issue

ReadableChunk is not a function

Please someone can help me ? i don't want to use multer storage cause I need to implement more than 10 files but it doesn't work i have only 2 files.

With that method I can see all my files but when the file is upload I can't read them.

0

There are 0 answers