在Nest.js中,当我想发送一个带有表单数据的请求时,该表单数据可能包含也可能不包含文件附件,那么我就无法在同一个请求中同时获得主体请求和文件,也无法验证主体请求的字段是否有效.
import {
Body,
Controller,
MaxFileSizeValidator,
ParseFilePipe,
Post,
UploadedFile,
UseInterceptors,
} from "@nestjs/common";
import { JoiValidationPipe } from "../../pipes/validation/joi-validation.pipe";
import { UserValidatorSchema } from "./validator/account-register.validator";
import { CreateUserDto } from "./dtos/account-register.dto";
import { AccountRegisterService } from "./account-register.service";
import { FileInterceptor } from "@nestjs/platform-express";
import { MAX_FILE_SIZE } from "../../constants/multer.constants";
@Controller("user")
export class FormdataWithFileController {
constructor(private readonly _formdataWithFileService: formdataWithFileService) {}
@Post("formdataWithFile")
@UseInterceptors(FileInterceptor("file"))
private async Signup(
@UploadedFile(
new ParseFilePipe({
validators: [
new MaxFileSizeValidator({ maxSize: MAX_FILE_SIZE })
],
})
)
file,
) {
return await this._formdataWithFileService.execute(file);
}
}