File Uploads
Nova supports multipart file uploads out of the box using the @File decorator. Files are handled via @fastify/multipart.
Basic Usage
upload.controller.ts TypeScript
import { Controller, Post, File, MultipartFile } from '@abrahambass/nova';
@Controller('/uploads')
class UploadController {
@Post('/avatar')
async uploadAvatar(
@File('avatar') file: MultipartFile,
) {
return {
filename: file.filename,
mimetype: file.mimetype,
encoding: file.encoding,
};
}
} Optional Files
Example TypeScript
@Post('/profile')
async updateProfile(
@File('avatar') avatar: MultipartFile,
@File('cover', { optional: true }) cover?: MultipartFile,
) {
// `avatar` is required, `cover` is optional
} 💡 Note
The request must have Content-Type: multipart/form-data. If not, a validation error is returned with message: "Content-Type must be multipart/form-data".
MultipartFile Properties
| Property | Type | Description |
|---|---|---|
filename | string | Original filename |
mimetype | string | MIME type (e.g. image/png) |
encoding | string | Encoding (e.g. 7bit) |
fieldname | string | Name of the form field |
file | Readable | Stream to read file contents |
toBuffer() | Promise<Buffer> | Read the entire file as a buffer |