O
O
oskar barin2021-12-19 23:28:10
Express.js
oskar barin, 2021-12-19 23:28:10

How to return zip file from post request in express.js?

Doesn't handle post response correctly in express.js.
On post /download/excel I want to create an excel file, pack it into an archive and upload it in the archive.
Now the file is being created, but when I download the file, the file does not open

const express = require('express')
const excel = require('exceljs')
const bodyParser = require('body-parser')
const app = express()
const spawn = require('child_process').spawn

app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());
app.use(bodyParser.raw());

const reportFields = [
  {
    title: "Время создания",
    description: "10.12.2021",
  },
  {
    title: "Title",
    description: "Description",
  },
];

app.post('/download/excel', (req, res) => {

  let workbook = new excel.Workbook();
  let worksheet = workbook.addWorksheet("Report");

  worksheet.columns = [
    { header: "Title", key: "title", width: 25 },
    { header: "Description", key: "description", width: 25 },
  ];

  worksheet.addRows(reportFields);

  res.setHeader(
    "Content-Type",
    "application/zip"
  );
  res.setHeader(
    "Content-Disposition",
    "attachment; filename=archive.zip"
  );


  workbook.xlsx.writeFile("report.xlsx").then(() => {
    console.log("xlsx file  report is written.");
    zip = spawn('zip',['-P', '12345' , 'archive.zip', 'report.xlsx']);
    zip.on('exit', function(code) {
       res.end(zip);
    });
  })
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ivanov, 2015-07-29
@litlleidiot

somehow the only thing is that when you click on "6", "block 7" is first shown, but I showed you the solution.
This is already your mistake. Since on yours when you click on "3" , "block 5" is shown

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question