NodeJS Nestjs CSV上传一直给我一个415“不支持的媒体类型”错误

xyhw6mcr  于 2023-05-22  发布在  Node.js
关注(0)|答案(1)|浏览(136)

我正在尝试编写一个支持CSV下载的路由。我试过两种方法:

@Post('/bulk')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile() file: Express.Multer.File) {
    console.log(file);
  }

@Post('/bulk')
  uploadFile(
    @Body(new ParseArrayPipe({ items: CreatePlaceDto })) dtos: CreatePlaceDto[], 
    @Req() request: CrudRequest) {
      console.log(dtos);
    }

但这两种方式都给我一个错误,说媒体类型不支持。

{
    "statusCode": 415,
    "code": "FST_ERR_CTP_INVALID_MEDIA_TYPE",
    "error": "Unsupported Media Type",
    "message": "Unsupported Media Type: multipart/form-data; boundary=--------------------------520916914275921544205184"

我试图通过Postman进行测试,我将头中的content-type设置为“multipart/form-data”,并在身体中,在form-data下,我将文件附加在关键字“file”中。
发生这种情况的原因可能是什么?任何指针将不胜感激!

hgb9j2n6

hgb9j2n61#

如果你像错误提示的那样在fastify中使用nestjs,那么Fastify只解析application/json。你需要包含这两个插件:
1.固结体
1.多部分加固
see this stackoverflow question, reference

相关问题