如何发送带有正文的 swagger multer 文件

问题描述 投票:0回答:1

我有一个 Nest js 项目,其端点接受带有正文的文件。 它适用于邮递员,但是 当我添加 swagger @ApiParam 时,它不会将文件发送到该字段

我的终点:

@ApiParam({ name: 'body', type: SendMessageParamsBody, description: 'body params ...' })
    @ApiParam({ name: 'attachments', type: 'file', description: 'file attachments' })
    @ApiOperation({ summary: 'send email' })
    @ApiResponse({ status: 200, description: 'sends email - main end point ' })
    @Post('send-email')
    @UseGuards(ThrottlerGuard)
    @UseInterceptors(FileFieldsInterceptor([
        {name: 'attachments', maxCount: 4}, //must be same as the field name
    ]))
    async sendEmail(@Body() body: SendMessageParamsBody,
                    @UploadedFiles() attachments: {attachment: Express.Multer.File[]},   //undefined when not passed
    ): Promise<MyApiResponse> {
        return await this.notificationService.handleSendMessageRequest(body, attachments?.attachments, MediumType.EMAIL)
    }

我没有收到任何文件。

知道如何让 swagger 将文件传递到“附件”字段吗? 谢谢

nestjs swagger
1个回答
0
投票

您可以使用控制器上的

ApiBody
装饰器来启用 swagger 中的文件上传。

您可以根据您的情况在正文中定义您的属性,它是

attachment

@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
  type: 'object',
  properties: {
    file: {
      type: 'string',
      format: 'binary',
    },
  },
},
}) 
@UseInterceptors(FileInterceptor('file'))

这就是它在招摇中的显示方式。 (确保重新加载swagger)

您可以在ApiBody

的官方文档中阅读更多相关内容
© www.soinside.com 2019 - 2024. All rights reserved.