Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
ExpressJS: Requesting/Downloading xlsx file using express returning .txt

My server side using Express JS

const XLSX = require('xlsx');

module.exports = () => {
  return async (req, res, next) => {
    try {
      
      const data = [['1', '2', '3'],['4', '5', '6']];

      const filename = `Testing.xlsx`;

      const wb = XLSX.utils.book_new();
      const ws = XLSX.utils.aoa_to_sheet(data);
      const merge = [
        { s: { r: 0, c: 0 }, e: { r: 0, c: 9 } },
        { s: { r: 1, c: 1 }, e: { r: 1, c: 7 } },
        { s: { r: 2, c: 1 }, e: { r: 2, c: 7 } },
        { s: { r: 2, c: 8 }, e: { r: 3, c: 9 } },
        { s: { r: 3, c: 0 }, e: { r: 4, c: 0 } },
        { s: { r: 3, c: 1 }, e: { r: 4, c: 1 } }
      ];
      ws['!merges'] = merge;
      XLSX.utils.book_append_sheet(wb, ws, 'Detail');
      const wbBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });

      res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8');
      res.attachment(filename);
      return res.status(200).send(wbBuffer);
    } catch (error) {
      logger.log('error', 'DownloadSellerFinancialAccounts', { error });
      return next(error);
    }
  };
};

Meanwhile in the client site

export function apiDownloadFinancilJournal() {
  return axios.get(`${paymentUrlApi.downloadFinancialJournal}`, { responseType: 'blob' });
}



apiDownloadFinancilJournal()
  .then((res) => {
    const url = window.URL.createObjectURL(new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' }));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', `Financial-Journal.xlsx`);
    document.body.appendChild(link);
    link.click();
  })
  .catch((err) => {
    this.setState({ errorMessage: err.response?.data?.message ?? 'Server Error' });
  });
};

My question is, in which side I did it wrong? because the result is a txt instead of xlsx file. I have tried different type of stream data (blob, arrayBuffer, buffer) but the result is even worse

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!