@fastify/multipart decorators for Nest.js
npm install nest-file-fastifybash
$ npm install nest-file-fastify @fastify/multipart
`
Yarn
`bash
$ yarn add nest-file-fastify @fastify/multipart
`
pnpm
`bash
$ pnpm install nest-file-fastify @fastify/multipart
`
并在您的 Nest.js 应用程序中注册 multipart 插件
`typescript
import fastyfyMultipart from '@fastify/multipart';
...
app.register(fastyfyMultipart);
`
文档
$3
FileInterceptor 参数:
- fieldname: string - 包含文件的字段的名称
- options: 可选的 UploadOptions 类型对象
`ts
import { FileInterceptor, UploadedFile, MemoryStorageFile } from 'nest-file-fastify';
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
uploadFile(@UploadedFile() file: MemoryStorageFile) {
console.log(file);
}
`
$3
FilesInterceptor 参数:
- fieldname: string - 包含文件的字段的名称
- maxCount: number - 可选的数字 - 接受的文件的最大数量
- options: 可选的 UploadOptions 类型对象
`ts
import { FilesInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';
@Post('upload')
@UseInterceptors(FilesInterceptor('files'))
uploadFile(@UploadedFiles() files: MemoryStorageFile[]) {
console.log(files);
}
`
$3
FileFieldsInterceptor 参数:
- uploadFields: 类型为 UploadField 的数组对象
- options: 可选的 UploadOptions 类型对象
`ts
import { FileFieldsInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';
@Post('upload')
@UseInterceptors(FileFieldsInterceptor([
{ name: 'avatar', maxCount: 1 },
{ name: 'background', maxCount: 1 },
]))
uploadFile(@UploadedFiles() files: { avatar?: MemoryStorageFile[], background?: MemoryStorageFile[] }) {
console.log(files);
}
`
$3
AnyFilesInterceptor 参数:
- options: 可选的 UploadOptions 类型对象
`ts
import { AnyFilesInterceptor, UploadedFiles, MemoryStorageFile } from 'nest-file-fastify';
@Post('upload')
@UseInterceptors(AnyFilesInterceptor()
uploadFile(@UploadedFiles() files: MemoryStorageFile[]) {
console.log(files);
}
``