richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 259 forks source link

Using a useTempFiles: true results in a empty buffer #354

Closed shreekrishnalamichhane closed 10 months ago

shreekrishnalamichhane commented 11 months ago

My environment

express: ^4.18.2 express-fileupload: ^1.4.0 node: v18.17.0 os: Ubuntu 23.04 -=================================

Issue

When useTempFiles: false, file buffer has data. image

When useTempFiles: true, file buffer has no content and buffer size is 0. image

-=================================

My middleware config

app.use(
  fileUpload({
    useTempFiles: true,
    tempFileDir: './tmp/',
    abortOnLimit: true,
    limits: { fileSize: 50 * 1024 * 1024, files: 1 },
    responseOnLimit: 'File size limit has been reached',
    uploadTimeout: 10000, // 10 seconds
  })
);

My controller code

check: async (req: Request, res: Response): ExpressResponse => {
    try {
      const file = req.files?.file as UploadedFile | null | undefined;

      if (!file) throw new CustomError('File not found', 400);

      console.log(file);
      console.log('Buffer Length:', file.data.length);

    } catch (error) {
      console.log(error);
    }
}
RomanBurunkov commented 10 months ago

Hi,

This is why useTempFiles options had been created: to not store uploads in the memory. So it works as designed.