NestJS validate total size of uploaded files

1.2k views Asked by At

I'm trying to upload a bunch of attachments in my NestJS project. It's an array of multiple files and uploaded like so

  @Post('/text')
  async addText(@UploadedFiles() files){
    console.log ("The files", files)
  }

How do I ensure that the total size of the all the attachments do not exceed say 5MB? Is there a way to validate all the files?

1

There are 1 answers

0
Hai Alison On

in document has mention this validation, or you can use Multer config to validate the size in your module like this:

  imports: [
MulterModule.registerAsync({
  useFactory: () => ({
    // other config
    limits: {
      fileSize: parseInt(process.env.MAX_SIZE_PER_FILE_UPLOAD),
      files: parseInt(process.env.MAX_NUMBER_FILE_UPLOAD),
    },
  }),
}),
//other code
]