skip to Main Content

I want to upload file and use dto to validate other things in nest
but when send request by post man I get bad request (that’s from dto ) and when I remove send file everything works fine

This is my controller :

@Post()
  @UseInterceptors(FileInterceptor('image', SaveImageToStorage))
  // @UploadedFile() file: Express.Multer.File ,
  create(
    @Body() createTicketDto: CreateTicketDto,
    @Request() req,
  ) {
    try {
      // console.log(image);
      console.log(createTicketDto);
      // return this.ticketService.create(createTicketDto)  ;
    } catch (error) {
      console.log(error);
    }
}

and this is the error after sending my request from postman :

{
    "message": "Bad Request",
    "statusCode": 400
}

postmanimage

I cant upload image and get all files !

I need an answer why this is happening

2

Answers


  1. Is it possible to have a screen of your postman request ?

    Login or Signup to reply.
  2. First of all, check your imports, maybe problem in them.

    FileInterceptor import from "@nestjs/platform-express", UseInterceptors from "@nestjs/common".

    Remove file from your dto, if it exist. File you are going to get using this way: @UploadedFile() file: Express.Multer.File. Use DTO for other things.

    Your SaveImageToStorage gotta look like this:

    {
    storage: diskStorage({
      destination: './uploads',
      filename: (req, file, cb) => {
        const name = file.originalname.split('.')[0];
        const fileExtenstion = file.originalname.split('.')[1];
        const newFileName = name.split(" ").join('_')+'_'+Date.now()+'.'+fileExtenstion;
    
        cb(null, newFileName);
      }
     }),
    }
    

    import {diskStorage} from "multer";
    and
    import {extname} from "path";

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search