I'm working with Nodejs/NestJs, and now I'm trying to generate an xlsx file (at this point, I'm not sure if my code is right), and now I want to send this file as the response, but I don't know how to perform it. I read about the @Res decorator, but I don't resources to attach a file on response.
Do you have any idea? Thanks :)
Service:
downloadFile(file: number) {
this.files
.findOne({
attributes: ['content'],
where: { id: file },
})
.then((response) => {
const book: XLSX.WorkBook = XLSX.read(response.content, {
type: 'base64',
});
return XLSX.writeFile(book, 'test.xlsx', { type: 'file' });
})
.catch((error) => {
console.log('some error: ', error);
});
}
Controller:
@Get('download/:file')
@Header('Content-type', 'multipart/form-data')
downloadFile(@Param('file') file: number, @Res() res: Response) {
this.fileService.downloadFile(file)
}