Complete the code to import the correct decorator for file upload in NestJS.
import { [1] } from '@nestjs/common';
The @UploadedFile() decorator is used to access the uploaded file in a controller method.
Complete the code to apply a file validation pipe in the controller method parameter.
uploadFile(@UploadedFile(new [1]()) file: Express.Multer.File) { return file; }
The ParseFilePipe is the built-in NestJS pipe for validating uploaded files.
Fix the error in the pipe options to allow only PNG files.
new ParseFilePipe({ validators: [ new FileTypeValidator({ fileType: '[1]' }) ] })The FileTypeValidator expects the MIME type string. For PNG files, it is image/png.
Fill both blanks to set a max file size of 1MB and allow only JPEG files.
new ParseFilePipe({ validators: [ new FileTypeValidator({ fileType: '[1]' }), new MaxFileSizeValidator({ maxSize: [2] }) ] })JPEG files have MIME type image/jpeg. 1MB in bytes is 1048576.
Fill all three blanks to create a custom validation pipe that checks file size and type.
export class CustomFileValidationPipe implements PipeTransform { transform(file: Express.Multer.File) { if (!file) throw new BadRequestException('No file uploaded'); if (file.mimetype !== '[1]') throw new BadRequestException('Invalid file type'); if (file.size > [2]) throw new BadRequestException('File too large'); return file; } } // Usage: @UploadedFile(new [3]())
The custom pipe checks for JPEG files (image/jpeg) and max size 1MB (1048576 bytes). It is used by passing its class name.