由于nestjs是一个快速应用程序,因此可以使用任何库来处理使用嵌套的上传,并且由于它提供了Midlewares,因此也可以使用multer。我的问题是:使用nestjs处理文件上传的最佳方法是什么?
正如@Kamyl在https://github.com/nestjs/nest/issues/262问题上所知,由于v4.6.0
可以使用公共文件拦截器使用multer上传文件到nestjs。
import { ... , UseInterceptors, FileInterceptor, UploadedFile } from '@nestjs/common'
...
@UseInterceptors(FileInterceptor('file'))
async upload( @UploadedFile() file) {
console.log(file)
}
这样变量file
将有一个buffer
它还需要字段名称作为第一个参数,然后是具有Multer选项的数组
import { ... , UseInterceptors, FileInterceptor, UploadedFile } from '@nestjs/common'
import { diskStorage } from 'multer'
import { extname } from 'path'
...
@UseInterceptors(FileInterceptor('file', {
storage: diskStorage({
destination: './uploads'
, filename: (req, file, cb) => {
// Generating a 32 random chars long string
const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('')
//Calling the callback passing the random name generated with the original extension name
cb(null, `${randomName}${extname(file.originalname)}`)
}
})
}))
async upload( @UploadedFile() file) {
console.log(file)
}
这样变量file
将有filename
,destination
和path
。
来自destination
的diskStorage
param也可以是一个函数,参数和期望回调与filename
相同。通过传递diskStorage
文件将自动保存到目的地通知给出的文件名。
使用@UploadedFiles
和FilesInterceptor
(复数)也可以处理多个文件
更简洁的方法是将配置提取到单独的文件,然后在拦截器方法中调用它
import { extname } from 'path';
import { existsSync, mkdirSync } from 'fs';
import { diskStorage } from 'multer';
import { v4 as uuid } from 'uuid';
import { HttpException, HttpStatus } from '@nestjs/common';
// Multer configuration
export const multerConfig = {
dest: process.env.UPLOAD_LOCATION,
};
// Multer upload options
export const multerOptions = {
// Enable file size limits
limits: {
fileSize: +process.env.MAX_FILE_SIZE,
},
// Check the mimetypes to allow for upload
fileFilter: (req: any, file: any, cb: any) => {
if (file.mimetype.match(/\/(jpg|jpeg|png|gif)$/)) {
// Allow storage of file
cb(null, true);
} else {
// Reject file
cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
}
},
// Storage properties
storage: diskStorage({
// Destination storage path details
destination: (req: any, file: any, cb: any) => {
const uploadPath = multerConfig.dest;
// Create folder if doesn't exist
if (!existsSync(uploadPath)) {
mkdirSync(uploadPath);
}
cb(null, uploadPath);
},
// File modification details
filename: (req: any, file: any, cb: any) => {
// Calling the callback passing the random name generated with the original extension name
cb(null, `${uuid()}${extname(file.originalname)}`);
},
}),
};
然后像拦截器一样在拦截器下调用它
import { ... , UseInterceptors, FileInterceptor, UploadedFile } from '@nestjs/common'
import { diskStorage } from 'multer'
import { extname } from 'path'
import { multerOptions } from 'src/config/multer.config';
...
@Post('/action/upload')
@UseInterceptors(FileInterceptor('file', multerOptions))
async upload( @UploadedFile() file) {
console.log(file)
}
一种简单的方法是使用控制器。您需要定义一个上传控制器并将其添加到您的app.module中,这是一个控制器应该是什么(后端)的示例:
@Controller()
export class Uploader {
@Post('sampleName')
@UseInterceptors(FileInterceptor('file'))
uploadFile(@UploadedFile() file) {
// file name selection
const path = `desired path`;
const writeStream = fs.createWriteStream(path);
writeStream.write(file.buffer);
writeStream.end();
return {
result: [res],
};
}
}
并通过前端fetch调用您的控制器:
fetch('controller address', {
method: 'POST',
body: data,
})
.then((response) => response.json())
.then((success) => {
// What to do when succeed
});
})
.catch((error) => console.log('Error in uploading file: ', error));