Skip to content

Instantly share code, notes, and snippets.

@Insidexa
Created April 4, 2020 07:48
Show Gist options
  • Save Insidexa/be332b068ba1df12af1907d229939cbb to your computer and use it in GitHub Desktop.
Save Insidexa/be332b068ba1df12af1907d229939cbb to your computer and use it in GitHub Desktop.
@Controller()
export class AppController {
@ApiBody({ type: BodyDto, description: 'desc' })
@ApiConsumes('multipart/form-data')
@UseInterceptors(FileInterceptor('fileUpload'), new FileRequestMapperInterceptor('fileUpload'))
@Post()
example(
@Body() body: BodyDto,
): BodyDto {
console.log(body) // exists fileUpload
return body;
}
}
class BodyDto {
@ApiProperty({ default: 'asd' })
public someField: string = 'asd';
@ApiProperty({ type: 'string', format: 'binary' })
public fileUpload: any;
}
@Injectable()
export class FileRequestMapperInterceptor implements NestInterceptor {
constructor(
private fieldName: string,
) {
}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const req = context.switchToHttp().getRequest();
req.body[this.fieldName] = req.file;
return next.handle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment