在Swagger NestJs中与其他数据一起上载文件

在Swagger NestJs中与其他数据一起上载文件,nestjs,nestjs-swagger,Nestjs,Nestjs Swagger,我想随JSON一起发送文件 { "comment" : "string", "outletId" : 1 } 我从文档中得到的帮助是 requestBody: content: multipart/form-data: schema: type: object properties: orderId:

我想随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,
    })
  }
@Post(“/punchin”)
@ApiConsumes(“多部分/表单数据”)
@ApiOperation({摘要:'考勤打卡'})
@UseInterceptors(CrudRequestInterceptor、ClassSerializerInterceptor、FileInterceptor('file'))
@apimplicitfile({name:'file'})
异步punchInAttendance(@Body()Body:PunchInDto,@UploadedFile()file:Express.Multer.file):承诺{
const imageUrl=等待此.s3FileUploadService.upload(文件)
console.log(body,imageUrl)
返回等待此.service.punchinattendence({
注释: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);
  }

我进入控制台:

{
  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
}

注释处出现错误,并指出
Type'字符串不可分配给类型'SchemaObject | ReferenceObject'。
尝试将其从
'string'
更改为
string
-js object这样做并不能解决问题。。。我分配了空对象
{}
只是为了测试。。错误消失了,但新的错误出现了,它说
错误TS2688:找不到'loash'的类型定义文件。
如果同时删除
注释:'string',outletId:'integer'
?删除注释和出口id会在swagger UI中将字段作为文件上载。现在告诉我如何包括评论和出口id了。
@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();
  }
}