文件上传以及 Swagger NestJs 中的其他数据

问题描述

我想和 JSON 一起发送文件

{
    "comment" : "string","outletId" : 1
}

我从文档中得到的帮助是

requestBody:
    content:
      multipart/form-data:
        schema:
          type: object
          properties:
            orderId:
              type: integer
            userId:
              type: integer
            fileName:
              type: string
              format: binary

我不知道把这个架构放在哪里。我曾尝试将它放在 DTO 和 @Apiproperty() 中的 @ApiOperations 中,但无法解决问题。

下面是我想在其中捕获文件内容函数

@Post('/punchin')
@ApiConsumes('multipart/form-data')
@ApiOperation({ summary: 'Attendance Punch In' })
@UseInterceptors(CrudRequestInterceptor,ClassSerializerInterceptor,FileInterceptor('file'))
@ApiImplicitFile({ name: 'file' })
async punchInAttendance( @Body() body: PunchInDto,@UploadedFile() file: Express.Multer.File ): Promise<Attendance> {
    const imageUrl = await this.s3FileUploadService.upload(file)
    console.log(body,imageUrl)
    return await this.service.punchInAttendance({
      comment: body.punchInComment,outletId: body.outletId,imgurl: imageUrl,})
  }

解决方法

使用 @ApiBody 因为 body 会保存您的数据。

  @Post('upload')
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',properties: {
        comment: { type: 'string' },outletId: { type: 'integer' },file: {
          type: 'string',format: 'binary',},})
  @UseInterceptors(FileExtender)
  @UseInterceptors(FileInterceptor('file'))
  uploadFile2(@UploadedFile('file') file) {
    console.log(file);
  }

screenshot

我进入控制台:

{
  fieldname: 'file',originalname: 'dart.txt',encoding: '7bit',mimetype: 'text/plain',buffer: <Buffer 20 0a 69 6d  ... 401 more bytes>,size: 451,comment: 'some comment',outletId: 123456
}

因为 FileInterceptor 删除了正文参数,所以我使用了 FileExtender 拦截器,将 commentoutletId 打包到文件属性中。

@Injectable()
export class FileExtender implements NestInterceptor {
  intercept(context: ExecutionContext,next: CallHandler): Observable<any> {
    const req = context.switchToHttp().getRequest();
    req.file['comment'] = req.body.comment;
    req.file['outletId'] = Number(req.body.outletId);
    return next.handle();
  }
}