【问题标题】:TSOA @UploadedFile won't accept fileTSOA @UploadedFile 不会接受文件
【发布时间】:2022-09-28 19:41:53
【问题描述】:

我定义了一个端点来接受multipart/form-data 文件,如下所示:

@Post(\'/...\')
@SuccessResponse(202, \'Accepted\')
@Response(400, \'Bad request\')
@Response(404, \'Not found\')
public async acceptFile(
    @UploadedFile(\'file\') file: Express.Multer.File
): Promise<...> {
    return this.fileService.acceptFile(file);
}

当我发送一个带有文件参数的multipart/form-data 请求时,TSOA 不接受这个请求,声称\"\'file\' is required\"。即使我提供了它:

如您所见,请求是由 Postman 创建的,是正确的并且具有 file 表单数据参数。根据TSOA file upload documentation,这应该可行。

    标签: typescript postman tsoa


    【解决方案1】:

    我无法解决这个问题,因此实施了一个解决方法:

    首先添加 multer 中间件来处理表单数据文件:

    const multerAny = multer({
          storage: multer.diskStorage()
        }).any();
    app.use(multerAny);
    

    然后我使用了 TSOA 也可以注入的原始快速请求并从中获取文件:

    @Post('/...')
    @SuccessResponse(202, 'Accepted')
    @Response(400, 'Bad request')
    @Response(404, 'Not found')
    public async acceptFile(
        @Request() request: Express.Request
    ): Promise<...> {
        //request.files will have file array, check if it has elements
        return this.fileService.acceptFile(request.files[0]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2014-09-10
      • 2015-09-16
      • 1970-01-01
      • 2020-02-04
      • 2014-10-17
      相关资源
      最近更新 更多