/
githubmirror
/
nest
Обзор
Документация
Войти
/
githubmirror
/
nest
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sample/29-file-upload/src/app.controller.ts
75 строк
2 KB
Chathula
fix: remove unused e2e test
09 апр 2025, 17:30
09 апр 2025, 17:30
0b07780
Код
Авторство
О чём код?
import { Body, Controller, Get, ParseFilePipeBuilder, Post, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { Express } from 'express'; import { AppService } from './app.service'; import { SampleDto } from './sample.dto'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() sayHello() { return this.appService.getHello(); } @UseInterceptors(FileInterceptor('file')) @Post('file') uploadFile( @Body() body: SampleDto, @UploadedFile() file: Express.Multer.File, ) { return { body, file: file.buffer.toString(), }; } @UseInterceptors(FileInterceptor('file')) @Post('file/pass-validation') uploadFileAndPassValidation( @Body() body: SampleDto, @UploadedFile( new ParseFilePipeBuilder() .addFileTypeValidator({ fileType: 'jpeg', }) .build({ fileIsRequired: false, }), ) file?: Express.Multer.File, ) { return { body, file: file?.buffer.toString(), }; } @UseInterceptors(FileInterceptor('file')) @Post('file/fail-validation') uploadFileAndFailValidation( @Body() body: SampleDto, @UploadedFile( new ParseFilePipeBuilder() .addFileTypeValidator({ fileType: 'jpg', }) .build(), ) file: Express.Multer.File, ) { return { body, file: file.buffer.toString(), }; } }