此库增加了装饰器 (原始内容存档于2019-09-21) (英语). Nest.js 支助 粘接多块. API与官方Nest.js Express文件装饰器非常相近.
安装
国家预防机制
$ npm install @blazity/nest-file-fastify fastify-multipart
雅伦
$ yarn add @blazity/nest-file-fastify fastify-multipart
文档
单个文件
import { FileInterceptor, UploadedFile, StorageFile } from '@blazity/nest-file-fastify';
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
uploadFile(@UploadedFile() file: StorageFile) {
console.log(file);
}
FileInterceptor 参数 :
-
fieldname: 字符串 - 持有文件的字段名称 -
options: 类型中的可选对象UploadOptions
文件阵列
import { FilesInterceptor, UploadedFiles, StorageFile } from '@blazity/nest-file-fastify';
@Post('upload')
@UseInterceptors(FilesInterceptor('files'))
uploadFile(@UploadedFiles() files: StorageFile[]) {
console.log(files);
}
FilesInterceptor 参数 :
-
fieldname: 字符串 - 持有文件的字段名称 -
maxCount: 可选数字 - 要接受的文件的最大数量 -
options: 类型中的可选对象UploadOptions
多个文件
import { FileFieldsInterceptor, UploadedFiles, StorageFile } from '@blazity/nest-file-fastify';
@Post('upload')
@UseInterceptors(FileFieldsInterceptor([
{ name: 'avatar', maxCount: 1 },
{ name: 'background', maxCount: 1 },
]))
uploadFile(@UploadedFiles() files: { avatar?: StorageFile[], background?: StorageFile[] }) {
console.log(files);
}
FileFieldsInterceptor 参数 :
-
uploadFields: 类型对象UploadField -
options: 类型中的可选对象UploadOptions
任何文件
import { AnyFilesInterceptor, UploadedFiles, StorageFile } from '@blazity/nest-file-fastify';
@Post('upload')
@UseInterceptors(AnyFilesInterceptor()
uploadFile(@UploadedFiles() files: StorageFile[]) {
console.log(files);
}
AnyFilesInterceptor 参数 :
options: 类型中的可选对象UploadOptions