Type Of Object Received During File Upload Using @uploadfile
In the REST API below, what is the type of file object that is received. @Post('/:folderId/documents/:fileName') @UseInterceptors(FileInterceptor('file')) @ApiConsumes('multipart/
Solution 1:
You can import the type from the package. '@types/multer'
and then qualify the file as:
@UploadedFile() file: Express.Multer.File,
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/multer/index.d.ts#L103
Solution 2:
You can save the file by specifying a destination path in the MulterOptions
:
// files will be saved in the /uploads folder@UseInterceptors(FileInterceptor('file', {dest: 'uploads'}))
If you want more control over how your file is saved, you can create a multer diskStorage
configuration object instead:
import { diskStorage } from'multer';
exportconst myStorage = diskStorage({
// Specify where to save the filedestination: (req, file, cb) => {
cb(null, 'uploads');
},
// Specify the file namefilename: (req, file, cb) => {
cb(null, Date.now() + '-' + file.originalname);
},
});
And then pass it to the storage
property in your controller.
@UseInterceptors(FileInterceptor('file', {storage: myStorage}))
For more configuration options, see the multer docs.
Solution 3:
try this...
@Post(':userid/avatar')
@UseInterceptors(FileInterceptor('file',
{
storage: diskStorage({
destination: './avatars',
filename: (req, file, cb) => {
const randomName = Array(32).fill(null).map(() => (Math.round(Math.random() * 16)).toString(16)).join('')
return cb(null, `${randomName}${extname(file.originalname)}`)
}
})
}
)
)
uploadAvatar(@Param('userid') userId, @UploadedFile() file) {
this.userService.setAvatar(Number(userId), `${this.SERVER_URL}${file.path}`);
}
check this out for more info - https://www.techiediaries.com/nestjs-upload-serve-static-file/
Post a Comment for "Type Of Object Received During File Upload Using @uploadfile"