Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda