I'm using @FilesInterceptor and @UploadedFiles from NestJS to upload files in my API. Here's part of the code :
@HttpCode(200)
@UseInterceptors(
FilesInterceptor('files', undefined, {
preservePath: true,
limits: {
fileSize: process.env.FILES_STORAGE_SIZE
? parseInt(process.env.FILES_STORAGE_SIZE)
: 1073741824, // 1GB
},
}),
)
async uploadFiles(
@UploadedFiles() files: Express.Multer.File[],
@UserReq('id') userId: number,
@Param('parentId') parentId: string | null,
)
The problem I encounter is that the file.originalname is not correctly encoded. If the file is named "Tést.pdf", I get "teÌst.pdf". I cannot find which parameter to set to fix the issue
One solution is to convert the file name after the upload
const fileNameEncoded = Buffer.from(file.originalname, 'latin1').toString(
'utf8',
);
But I would prefer using Multer options to fix the issue
Thanks for the help
To solve this issue in your NestJS app, you can provide MulterModule
with a fileFilter
defined as follow:
MulterModule.register({
fileFilter: (_, file, cb) => {
file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8');
cb(null, true);
},
}),
This will apply the workaround for every controllers in your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With