NestJs简单实现文件上传
简介
nestjs + swagger
file.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import { Module } from '@nestjs/common'; import { FileController } from './file.controller'; import { FileService } from './file.service'; import { MulterModule } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; import dayjs = require('dayjs'); import * as nuid from 'nuid';
@Module({ imports: [ MulterModule.register({ storage: diskStorage({ destination: `./public/uploads/${dayjs().format('YYYY-MM-DD')}`, filename: (req, file, cb) => { const filename = `${nuid.next()}.${file.mimetype.split('/')[1]}`; return cb(null, filename); }, }), }), ], controllers: [FileController], providers: [FileService], }) export class FileModule {}
|
file.controller.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import { Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiConsumes, ApiNotImplementedResponse, ApiBody } from '@nestjs/swagger'; import { FileInterceptor } from '@nestjs/platform-express'; import { FileUploadDto } from './dto/upload.dto';
@ApiTags('文件上传') @Controller('file') export class FileController {
@ApiOperation({summary: '文件上传,接收 multipart/form-data 的数据'}) @ApiConsumes('multipart/form-data') @ApiBody({ description: '文件上传', type: FileUploadDto, }) @UseInterceptors(FileInterceptor('file')) @Post('upload') uploadFile(@UploadedFile() file) { return file; } }
|
upload.dto.ts
1 2 3 4 5 6
| import { ApiProperty } from '@nestjs/swagger';
export class FileUploadDto { @ApiProperty({ type: 'string', format: 'binary' }) file: any; }
|
main.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import { join } from 'path';
async function bootstrap() { const app = await NestFactory.create(AppModule, { cors: true, });
app.use(express.json()); app.use(express.urlencoded({ extended: true }));
const rootDir = join(__dirname, '..'); app.use('/public', express.static(join(rootDir, 'public')));
app.setGlobalPrefix('/api'); app.useGlobalInterceptors(new TransformInterceptor()); app.useGlobalFilters(new AllExceptionFilter()); app.useGlobalFilters(new HttpExceptionFilter());
const options = new DocumentBuilder() .setTitle('角色权限管理') .setDescription('角色权限') .setVersion('1.0') .addBearerAuth() .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('doc', app, document);
await app.listen(port); Logger.log(`http://localhost:${port}`, '服务启动成功'); } bootstrap();
|
前端
1 2 3 4 5 6 7 8 9 10
| <template> <div class=""> <el-upload action="http://localhost:3000/api/file/upload" drag> <i class="el-icon-upload" /> <div class="el-upload__text"> 将文件拖到此处,或<em>点击上传</em> </div> </el-upload> </div> </template>
|
如有问题可联系 Email:afacode@outlook.com 或 微信:afacode