From 2ea1cf63bac15552c754375a38909885c96c7eeb Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 18 Mar 2021 01:26:41 +0300 Subject: feat: remove file after download --- src/services/spreadsheets.service.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/services/spreadsheets.service.ts b/src/services/spreadsheets.service.ts index b33a653..b52d47e 100644 --- a/src/services/spreadsheets.service.ts +++ b/src/services/spreadsheets.service.ts @@ -2,6 +2,7 @@ import { Application } from '@feathersjs/express'; import { ServiceMethods, Id } from '@feathersjs/feathers'; import ExcelJS from 'exceljs'; import _ from 'lodash'; +import fs from 'fs'; import { WaybillSchema } from '../models/waybill/waybill.schema'; const duplicateRowWithMergedCells = (sheet: any, row: number, count: number) => { @@ -57,13 +58,19 @@ class Spreadsheets implements Partial> { getCol('AP').value = record.price; }); - await workbook.xlsx.writeFile('./documents/waybill.xlsx'); - console.log('written!'); - return 'written!'; + const fileName = `./documents/${waybill._id}.xlsx`; + await workbook.xlsx.writeFile(fileName); + return fileName; } } export default (app: Application): void => { - app.use('/spreadsheets', new Spreadsheets); + app.use('/spreadsheets', new Spreadsheets, (req, res) => { + const fileName = res.data; + res.download(fileName, err => { + if (err) console.log(err); + fs.unlink(fileName, () => {}); + }); + }); }; -- cgit v1.2.3