nestjs-form-upload
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

@nestjs-form-upload

NestJS Form Upload is a module for NestJS that allows you to upload files using a form (multipart/form-data).

  • Process files nested in a form
  • Integration with class-validator and class-transformer
  • Support for multiple files
  • Process a single request fields and files at the same time
  • Support image resizing and image compression (using sharp)

Providers available

  • Memory (default)

Installation

$ npm install --save nestjs-form-upload

Usage

Import the module

import { Module } from "@nestjs/common";
import { FormUploadModule, FileUploadProvider } from "nestjs-form-upload";

@Module({
  imports: [
    FormUploadModule.register({
      provider: FileUploadProvider.MEMORY,
      options: {},
    }),
  ],
})
export class AppModule {}

Usage

Add the @FileUpload() decorator to your controller method. The decorator will parse the request and extract the files and fields.

import { Controller, Post, UseInterceptors } from "@nestjs/common";
import { FileInterceptor } from "nestjs-form-upload";
import { FileUploadService } from "nestjs-form-upload";

@Controller()
export class AppController {
  public constructor(private fileUploadService: AppService) {}

  @Post()
  @FileUpload({
    options: {
      extensions: ["jpg", "png"],
      maxFileSize: 1024 * 1024 * 5, // exprese in bytes = 5MB
      maxFiles: 5, // max files to upload at the same time
    },
  })
  public async create(@Body() file: FileUpload) {
    // Do something with the file

    return this.fileUploadService.create(file);
  }
}

Image resizing

You can resize the image using the resize option.

Note: The resize only works with images, if you try to resize a non-image file, the module will ignore the resize option and will upload the file as it is.

import { Controller, Post, UseInterceptors } from "@nestjs/common";

export class AppController {
  @Post()
  @FileUpload({
    options: {
      extensions: ["jpg", "png"],
      maxFileSize: 1024 * 1024 * 5, // exprese in bytes = 5MB
      maxFiles: 5, // max files to upload at the same time
      resize: {
        myImage: { width: 100, height: 100, quality: 90 },
      },
    },
  })
  public async create(@Body() file: FileUpload) {
    // Do something with the file
  }
}

Validation

If you want to validate the files, you can use the decorators.

Note: If need to validate an array of files, you need to use each: true property from ValidationOptions.

IsFile

Check if the file is a file.

@IsFile(validationOptions?: ValidationOptions)

IsFiles

Checks an array of files, the same as @IsFile({ each: true }) but with a better name.

@IsFiles(validationOptions?: ValidationOptions)

HasExtension

Check if the file has the specified extension.

@HasExtension(extensions: string, validationOptions?: ValidationOptions)

HasMime

Check if file has mime specified.

@HasMimeFile(mimes: string[])

MinFileSize

Check if the file has the minimum size.

@MinFileSize(minSize: number, validationOptions?: ValidationOptions)

MaxFileSize

Check if the file has the maximum size.

@MaxFileSize(maxSize: number, validationOptions?: ValidationOptions)

Package Sidebar

Install

npm i nestjs-form-upload

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

65.2 kB

Total Files

89

Last publish

Collaborators

  • maticapuano