Home > Net >  File download freezes and does not finish on linux
File download freezes and does not finish on linux

Time:04-30

I'm using "Node.js", "express" and "SheetJS" so that an endpoint that saves the data (from an array of objects) in an XLSX file and returns the url of the file to be downloaded by another endpoint as a static file.

import crypto from 'crypto';
import * as XLSX from 'xlsx';
import path from 'path';
import * as fs from 'fs';
...
const exportToExcelFile = async (data) => {
  ...
  const worksheet = XLSX.utils.json_to_sheet(data);

  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Data');

  const buf = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
  fs.writeFileSync(resolvedFilename, buf);

  return `${process.env.APP_URL}/public/downloads/${date}/${filename}`;
}

In Windows, the file generation and download work perfectly, however, when the application is running on the linux server the file is generated, however, the download freezes and does not finish.

[Download congelado][1]

If I change the 'buffer' type to 'binary', the download works on windows and linux, however, in both when trying to open the file, Excel shows a corrupted file message.

  const buf = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });

Any ideas or suggestions of what it could be?

CodePudding user response:

Does it help if you close the file after writing?

const fs = require("fs/promises");
(async function() {
  var file = await fs.open(resolvedFilename, "w");
  await file.write(buf);
  await file.close();
})();

CodePudding user response:

It works just fine, you can check your code live here: https://glitch.com/edit/#!/pentagonal-sepia-nutmeg
All I do is just copy/paste your code into glitch to see if it works. And it does.

So you should check your browser network tab, see if it reports any error. Also, take advantage of some tools such as curl with -v option to download the file, it will print all information about the download request you make

  • Related