#anyway to not use field name in multer nestjs
6 messages · Page 1 of 1 (latest)
Like, you want to send files on any field?
yeah, the idea is that the client side only need to send the file through my api without the need to specify the fieldname first (at default, the fieldname is 'file'). I just wonder whether or not it's possible
more info, i'm using nestjs homepage default example. here's the code
@Post('upload2')
@UseInterceptors(
FileInterceptor('file', {
limits: {
fileSize: 1024 * 1024 * 20, // 50MB limit (adjust as needed)
},
}),
)
async uploadFile(@UploadedFile() file: Express.Multer.File) {
// Check if a file was uploaded
if (file) {
const baseDirectory = process.cwd();
const filePath = ${baseDirectory}/video/${file.originalname};
// Save the file to the specified directory
fs.writeFileSync(filePath, file.buffer);
console.log(`File saved to: ${filePath}`);
} else {
console.log('No file was uploaded.');
}
}
It's possible, it's a horrible idea
I agree, but the story is it's a school project and my teammates insists on that approach. Therefore, could you please show me how to do it?
I would highly recommend not doing it but you can use the AbyFileInterceptor() to accept a file on any field and @UploadedFiles() to retrieve them from the req