英文:
NestJS: Option to make file in endpoint optional
问题
这段代码中的问题是文件上传字段被要求必须提供,而你希望它是可选的。你可以通过以下方式将上传文件字段变为可选:
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', { required: false })) // 将 required 参数设置为 false
async createPost(@Req() req: any, @Body() createPostDto: CreatePostDto, @UploadedFile(new ParseFilePipeBuilder().addMaxSizeValidator({ maxSize: 2048 }).build({ errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY })) file: Express.Multer.File) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
通过将 FileInterceptor
中的 required
参数设置为 false
,你可以使上传文件字段变为可选,不再必须提供。这样,如果没有提供文件,也不会再触发 "File is required" 的错误响应。
英文:
I am using NestJS to create REST API and I am trying to create an endpoint that will require certain body but also accept file that should be optional.
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', { }))
async createPost(@Req() req: any, @Body() createPostDto: CreatePostDto, @UploadedFile(new ParseFilePipeBuilder().addMaxSizeValidator({ maxSize: 2048 }).build({ errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY })) file: Express.Multer.File) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
But using this code makes the file required at all times as seen in this error response:
{
"statusCode": 422,
"message": "File is required",
"error": "Unprocessable Entity"
}
Is there a way to make the upload file optional?
答案1
得分: 1
I found an answer on NestJS discord. I changed ParseFilePipeBuilder to ParseFilePipe which supports fileIsRequired
option.
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', { }))
async createPost(@Req() req: any, @Body() createPostDto: CreatePostDto, @UploadedFile(new ParseFilePipe({
validators: [
new MaxFileSizeValidator({ maxSize: parseInt(process.env.MAX_FILE_UPLOAD_SIZE) * 1000 })
],
fileIsRequired: false
})) file?: Express.Multer.File) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
英文:
I found an answer on NestJS discord. I changed ParseFilePipeBuilder to ParseFilePipe which supports fileIsRequired
option.
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', { }))
async createPost(@Req() req: any, @Body() createPostDto: CreatePostDto, @UploadedFile(new ParseFilePipe({
validators: [
new MaxFileSizeValidator({ maxSize: parseInt(process.env.MAX_FILE_UPLOAD_SIZE) * 1000 })
],
fileIsRequired: false
})) file?: Express.Multer.File) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
答案2
得分: 0
Here is the translated code portion:
刚刚在整个网络上搜索后,找到了一个完美的工作解决方案。
只需将 `fileIsRequired: false` 添加到您的代码中的构建选项内:
.build({
fileIsRequired: false,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
所以您的更新后的代码将是:
enter code here
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', {}))
async createPost(
@Req() req: any,
@Body() createPostDto: CreatePostDto,
@UploadedFile(
new ParseFilePipeBuilder()
.addMaxSizeValidator({ maxSize: 2048 })
.build({
fileIsRequired: false,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
)
file: Express.Multer.File,
) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
英文:
Just found a perfect working solution for this this after searching the whole web.
just add fileIsRequired: false
to your code inside the build options:
.build({
fileIsRequired: false,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
So your updated code will be:
enter code here
@UseGuards(JwtAuthGuard)
@Post('create')
@UseInterceptors(FileInterceptor('file', {}))
async createPost(
@Req() req: any,
@Body() createPostDto: CreatePostDto,
@UploadedFile(
new ParseFilePipeBuilder()
.addMaxSizeValidator({ maxSize: 2048 })
.build({
fileIsRequired: false,
errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
}),
)
file: Express.Multer.File,
) {
return await this.postsService.createPost(req.user, createPostDto, file);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论