Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-form-upload

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-form-upload

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

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@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)

Keywords

FAQs

Package last updated on 29 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc