1.0.4 • Published 2 years ago

nestjs-file-upload v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

nestjs-file-upload

Upload files and perform validation in nestjs using decorators.

Table of content

Installation

Install using

npm i nestjs-file-upload

Usage

  1. Add the FileField decorator to the file fields in your dto.
import { Expose } from "class-transformer";
import { FileField, File } from "nestjs-file-upload";

export class MyDto {
    @Expose()
    @FileField()
    myFile: File;

    @Expose()
    anotherField: string;
}

The FileField can be combined with other validators, such as validaiton validator from class-validator.

import { Expose } from "class-transformer";
import { FileField, File } from "nestjs-file-upload";

export class MyDto {
    @Expose()
    @FileField()
    @IsNotEmpty()
    myFile: File;
}
  1. Add the FileInjector decorator to the endpoint.
import { Body, Controller, Post, ValidationPipe } from "@nestjs/common";

@Controller("my-controller")
export class MyController {
    @Post()
    @FileInjector(MyDto)
    public uploadFile(@Dto(ValidationPipe) dto: MyDto): void {
        console.log("Received file", dto.myFile.filename);
    }
}

Arguments

An argument of type fileFieldOptions can be passed to the FileField decorator.

ArgumentTypeDescription
fieldnamestringOverrides the property key. Can be used if the field in the form has a different name.
allowedMimeTypesstring[]Array of allowed mime types. Mime type validation is based on the reported type in the form. We may use magic numbers in the future.
maxSizenumberMaximum file size in bytes.
maxFilenumberMaximum number of files. If the number is 1, the property will have a value of File. If the value if greater than 1, the property will have a value of File[].

Example

An example is available in the example.

License

See the LICENSE for more info.

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago