Skip to content

Commit 173c1b9

Browse files
Merge branch 'Tony133-docs/file-upload'
2 parents 964f650 + 565367c commit 173c1b9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

content/techniques/file-upload.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ To handle file uploading, Nest provides a built-in module based on the [multer](
44

55
> warning **Warning** Multer cannot process data which is not in the supported multipart format (`multipart/form-data`). Also, note that this package is not compatible with the `FastifyAdapter`.
66
7+
For better type safety, let's install Multer typings package:
8+
9+
```shell
10+
$ npm i -D @types/multer
11+
```
12+
13+
With this package installed, we can now use the `Express.Multer.File` type (you can import this type as follows: `import {{ '{' }} Express {{ '}' }} from 'express'`).
14+
715
#### Basic example
816

917
To upload a single file, simply tie the `FileInterceptor()` interceptor to the route handler and extract `file` from the `request` using the `@UploadedFile()` decorator.
@@ -12,7 +20,7 @@ To upload a single file, simply tie the `FileInterceptor()` interceptor to the r
1220
@@filename()
1321
@Post('upload')
1422
@UseInterceptors(FileInterceptor('file'))
15-
uploadFile(@UploadedFile() file) {
23+
uploadFile(@UploadedFile() file: Express.Multer.File) {
1624
console.log(file);
1725
}
1826
@@switch
@@ -47,7 +55,7 @@ When using `FilesInterceptor()`, extract files from the `request` with the `@Upl
4755
@@filename()
4856
@Post('upload')
4957
@UseInterceptors(FilesInterceptor('files'))
50-
uploadFile(@UploadedFiles() files) {
58+
uploadFile(@UploadedFiles() files: Express.Multer.File) {
5159
console.log(files);
5260
}
5361
@@switch
@@ -102,7 +110,7 @@ When using `AnyFilesInterceptor()`, extract files from the `request` with the `@
102110
@@filename()
103111
@Post('upload')
104112
@UseInterceptors(AnyFilesInterceptor())
105-
uploadFile(@UploadedFiles() files) {
113+
uploadFile(@UploadedFiles() files: Express.Multer.File) {
106114
console.log(files);
107115
}
108116
@@switch

0 commit comments

Comments
 (0)