Skip to content

Commit

Permalink
Change VSR export function to send file directly to response instead …
Browse files Browse the repository at this point in the history
…of saving to disk (doesn't work on Vercel)
  • Loading branch information
benjaminJohnson2204 committed Apr 18, 2024
1 parent 8f6902f commit a410612
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions backend/src/controllers/vsr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RequestHandler } from "express";
import { RequestHandler, Response } from "express";
import { validationResult } from "express-validator";
import fs from "fs";
import createHttpError from "http-errors";
import FurnitureItemModel, { FurnitureItem } from "src/models/furnitureItem";
import VSRModel, { FurnitureInput, VSR } from "src/models/vsr";
Expand Down Expand Up @@ -205,7 +204,7 @@ const stringifySelectedFurnitureItems = (
.join(", ");
};

const writeSpreadsheet = async (filename: string) => {
const writeSpreadsheet = async (filename: string, res: Response) => {
const workbook = new ExcelJS.Workbook();

workbook.creator = "PAP Inventory System";
Expand Down Expand Up @@ -286,17 +285,19 @@ const writeSpreadsheet = async (filename: string) => {
});

// Write to file
await workbook.xlsx.writeFile(filename);
await workbook.xlsx.write(res);
};

export const bulkExportVSRS: RequestHandler = async (req, res, next) => {
try {
const filename = "vsrs.xlsx";
await writeSpreadsheet(filename);
res.download(filename, () => {
// Once the flie has been sent to the requestor, remove it from our filesystem
fs.unlinkSync(filename);
// Set some headers on the response so the client knows that a file is attached
res.set({
"Content-Disposition": `attachment; filename="${filename}"`,
"Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
});

await writeSpreadsheet(filename, res);
} catch (error) {
next(error);
}
Expand Down

0 comments on commit a410612

Please sign in to comment.