Created
April 4, 2020 07:48
-
-
Save Insidexa/be332b068ba1df12af1907d229939cbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BodyDto { | |
@ApiProperty({ default: 'asd' }) | |
public someField: string = 'asd'; | |
@ApiProperty({ type: 'string', format: 'binary' }) | |
public fileUpload: any; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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