'How to upload Files and Other fields with Request Body in Nestjs

I want to upload three fields like:

Example Data:

partner_id: 3638,
review: [{'product_id': 155, 'order_sku_id': 155, 'review_title': 'Orange Review','rating': 5 }],
review_images[0][0]: ImageFile00
review_images[0][1]: ImageFile01
review_images[1][0]: ImageFile10

review_images is a array of images like the above.

My approach to upload these data. I have created a Dto:

export class CreateReviewDto {
    partner_id : Number;
    review: any[];
    review_images: any[];
}

Controller:

@Controller('api/v1')
export class ReviewController {
  constructor(private readonly reviewService: ReviewService) {}

  @Post('/review')
  @HttpCode(201)
  @UseInterceptors(FilesInterceptor('review_images[]'))
  create(@Body() createReviewDto: CreateReviewDto, @UploadedFiles() review_images: Express.Multer.File) {
    return this.reviewService.create(params,createReviewDto);
  }
}

But images are not coming in POSTMAN. Giving undefined Am I going to a right way? What should I do?



Solution 1:[1]

The first thing that I noticed. You should use 'review_images' in your FilesInterceptor (link on documentation)

@Controller('api/v1')
export class ReviewController {
  constructor(private readonly reviewService: ReviewService) {}

  @Post('/review')
  @HttpCode(201)
  @UseInterceptors(FilesInterceptor('review_images'))
  create(@Body() createReviewDto: CreateReviewDto, @UploadedFiles() review_images: Express.Multer.File) {
    return this.reviewService.create(params,createReviewDto);
  }
}

The second thing. You should send the right request from postman. I suppose it should look like this enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oro