Search code examples
typescriptpostmantsoa

TSOA @UploadedFile won't accept file


I have an endpoint defined to accept a multipart/form-data file like so:

@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);
}

When I send a multipart/form-data request with a file parameter, TSOA does not accept this request, claiming that "'file' is required". Even though I provided it:

enter image description here

As you can see the request is created by Postman, is correct and has the file form data parameter. According to TSOA file upload documentation this should work.


Solution

  • I was unable to solve this and so implemented a workaround:

    First added multer middleware to handle form-data files:

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

    Then I used raw express request that TSOA can also inject and got the file from that:

    @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]);
    }