Multer

Installation

Before using the @MultipartFile() you must install multer and @tsed/multipartfile module on your project:

npm install --save multer @types/multer @tsed/multipartfiles
1

Configure the File upload directory

By default the directory used is ${projetRoot}/uploads. You can configure another directory on your ServerLoader settings.

import {ServerLoader, ServerSettings} from "@tsed/common";
import "@tsed/multipartfiles";
import Path = require("path");
const rootDir = Path.resolve(__dirname);

@ServerSettings({
   rootDir,
   mount: {
      '/rest': `${rootDir}/controllers/**/**.js`
   },
   uploadDir: `${rootDir}/custom-dir`,
   componentsScan: [
       `${rootDir}/services/**/**.js`
   ],
   
   multer: {
       // see multer options
   }
})
export class Server extends ServerLoader {

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Options

Example

Ts.ED use multer to handler file uploads. Single file can be injected like this:

import {Controller, Post} from "@tsed/common";
import {MultipartFile, MulterOptions} from "@tsed/multipartfiles";

@Controller('/')
class MyCtrl {
    
  @Post('/file')
  private uploadFile(@MultipartFile('file') file: Express.Multer.File) {

  }
     
  @Post('/file')
  @MulterOptions({dest: "/other-dir"})
  private uploadFile(@MultipartFile('file') file: Express.Multer.File) {
         
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

For multiple files, just add Array type annotation like this:

import {Controller, Post} from "@tsed/common";
import {MultipartFile} from "@tsed/multipartfiles";

@Controller('/')
class MyCtrl {
  @Post('/files')
  private uploadFile(@MultipartFile("files", 4) files: Express.Multer.File[]) {
    // multiple files with 4 as limits
  }
}
1
2
3
4
5
6
7
8
9
10

::: warning Swagger spec (v2.0) doesn't support multiple files. :::

::: tip You can find a working example on Multer here. :::