Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import { diskStorage } from "multer";
import { FileInterceptor } from "@nestjs/platform-express";
import { ApiResponse, ApiConsumes, ApiBody } from "@nestjs/swagger";
import {
Res,
Get,
Post,
Param,
Controller,
UploadedFile,
UseInterceptors,
} from "@nestjs/common";
@Controller("image")
export class ImageController {
@Get(":name")
@ApiResponse({ type: Buffer })
async serveAvatar(@Param("name") name, @Res() response): Promise<any> {
response.sendFile(name);
}
@Post()
@UseInterceptors(
FileInterceptor("file", {
storage: diskStorage({
filename: (_request, file, callback) =>
callback(null, `${new Date().getTime()}-${file.originalname}`),
}),
}),
)
@ApiBody({
required: true,
type: "multipart/form-data",
schema: {
type: "object",
properties: {
file: {
type: "string",
format: "binary",
},
},
},
})
@ApiConsumes("multipart/form-data")
async post(@UploadedFile() file): Promise<void> {
console.log(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.