Javascript Nest.js:使用multer处理多个文件
在“干净”的node.js框架(如Express.js)上开发时,我们可以轻松地处理文件处理、回调、验证等。但是使用Nest.js->对于简单的问题来说,它变得更简单了,但是当您想要调整它时,它会变得更复杂 我想在服务器上上传多个文件,然后立即调用另一个服务操作(如异步处理文件中的数据等)。Nest.js正在为此使用multer。我被卡住了Javascript Nest.js:使用multer处理多个文件,javascript,node.js,express,nestjs,Javascript,Node.js,Express,Nestjs,在“干净”的node.js框架(如Express.js)上开发时,我们可以轻松地处理文件处理、回调、验证等。但是使用Nest.js->对于简单的问题来说,它变得更简单了,但是当您想要调整它时,它会变得更复杂 我想在服务器上上传多个文件,然后立即调用另一个服务操作(如异步处理文件中的数据等)。Nest.js正在为此使用multer。我被卡住了 import { Controller, HttpException, HttpStatus, Post, UploadedFiles, UseInterc
import { Controller, HttpException, HttpStatus, Post, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { v4 as uuid } from 'uuid';
@Controller('reader')
export class ReaderController {
@Post('upload')
@UseInterceptors(
AnyFilesInterceptor({
storage: diskStorage({
destination: './uploads',
fileSize: 5e7,
files: 20,
filename: (req, file, cb) => {
try {
const fileName = uuid();
return cb(null, `${fileName}${extname(file.originalname)}`);
} catch (err) {
return cb(new HttpException('Errored at upload', HttpStatus.BAD_REQUEST));
}
}
}),
fileFilter: (req: any, file: any, cb: any) => {
if (file.mimetype.match(/\/(png)$/)) {
cb(null, true);
} else {
cb(
new HttpException(
`Unsupported file type ${extname(file.originalname)}`,
HttpStatus.BAD_REQUEST
),
false
);
}
}
})
)
uploadFile(@UploadedFiles() files) {
console.log('files', files); // <--- here
}
}
async.each(req.files, function(file, callback) {
saveFile(file, file.name, callback)
}, function(err) {
res.send('')
})