【问题标题】:File uploading along with other data in Swagger NestJs文件与 Swagger NestJs 中的其他数据一起上传
【发布时间】:2021-06-10 18:48:07
【问题描述】:

我想连同 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,
    })
  }

【问题讨论】:

    标签: nestjs nestjs-swagger


    【解决方案1】:

    使用@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
    }
    

    因为FileInterceptor 删除了body 参数,所以我使用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();
      }
    }
    

    【讨论】:

    • Type 'string is not assignable to type 'SchemaObject | ReferenceObject'.Type 'string is not assignable to type 'SchemaObject | ReferenceObject'.的评论和outletId处给出错误
    • 尝试将其从 'string' 更改为 String - js 对象
    • 这样做并不能解决问题...我分配空对象{ } 只是为了测试.. 错误消失了,但新的对象说error TS2688: cannot find type definition file for 'loash'.
    • 如果同时删除comment : 'string', outletId : 'integer' 是否有效?
    • 删除评论和出口 id 使字段成为招摇 UI 中的文件上传。现在告诉我如何包含评论和出口ID。
    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2023-03-30
    • 2015-02-20
    • 2014-01-14
    • 2011-05-31
    • 2018-01-23
    • 2013-04-14
    • 2022-12-06
    相关资源
    最近更新 更多