我使用Express的typescript和TSOA构建了一个Rest API。在一个路由中,我尝试接收多个表单字段(使用装饰器@FormField)和多个文件字段(使用装饰器@UploadedFile)。
查看官方链接:https://tsoa-community.github.io/docs/file-upload.html
当我只定义一个文件时,路由工作正常,但当我添加第二个文件字段时,Multer抱怨错误:
MulterError: Unexpected field
at wrappedFileFilter (*******node_modulesmulterindex.js:40:19)
at Busboy.<anonymous> (*******node_modulesmulterlibmake-middleware.js:115:
7)
at Busboy.emit (node:events:526:28)
at Busboy.emit (node:domain:475:12)
at Busboy.emit (*******node_modulesbusboylibmain.js:38:33)
at PartStream.<anonymous> (*******node_modulesbusboylibtypesmultipart.js:
213:13)
at PartStream.emit (node:events:526:28)
at PartStream.emit (node:domain:475:12)
at HeaderParser.<anonymous> (*******node_modulesdicerlibDicer.js:51:16)
at HeaderParser.emit (node:events:526:28) {
code: 'LIMIT_UNEXPECTED_FILE',
field: 'banner',
storageErrors: []
}
这是控制器代码:
@Post('')
public async create(
@Request() request: any,
@FormField() label: string,
@FormField() email: string,
@UploadedFile('logo') logo?: Express.Multer.File,
@UploadedFile('banner') banner?: Express.Multer.File,
): Promise<ResponseType> {
try {
const ns = await this.service.create({
label,
email,
},
request.user,
);
return { data: ns, success: true, detail: 'success' };
} catch (e: any) {
console.error(e);
this.setStatus(400);
return { detail: 'Validation Failed for Request Data : ' + e.message, success: false };
}
}
我认为这可能是Multer在请求中只期望一个文件的错误。那么,如何使用TSOA指定请求中有两个具有不同字段的文件呢?
没有收到答复,即使我在github上发布了一个问题,遗憾的是,是时候离开TSOA了。