Skip to content

Instantly share code, notes, and snippets.

@alexwhin
Last active November 28, 2023 11:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexwhin/ed13f34d0bae0449ddd9fb7e82cfeacd to your computer and use it in GitHub Desktop.
Save alexwhin/ed13f34d0bae0449ddd9fb7e82cfeacd to your computer and use it in GitHub Desktop.
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);
}
}
@dayachettri
Copy link

Thanks man! was searching how to implement upload in swagger for hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment