文件下载冻结,未在Linux上完成

发布于 2025-01-25 00:55:53 字数 891 浏览 3 评论 0原文

我正在使用“ node.js”,“ express”和“ sheetJs”静态文件。

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}`;
}

但是,在Windows中,文件生成和下载完美地工作,但是,当应用程序在Linux服务器上运行时,文件的生成了,但是下载冻结并且未完成。

[下载Congelado] [1]

如果我将“缓冲区”类型更改为“二进制”,则下载在Windows和Linux上工作,但是,在尝试打开文件时,Excel都会显示损坏的文件消息。

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

关于它可能是什么的想法或建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2025-02-01 00:55:54

如果您在写作后关闭文件会有所帮助吗?

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

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();
})();
云淡风轻 2025-02-01 00:55:54

它可以正常工作,您可以在此处检查您的代码: https:// glitch。 com/edit/#!/pentagonal-sepia-nutmeg
我要做的就是将您的代码复制/粘贴到小故障中,以查看它是否有效。而且确实如此。

因此,您应该检查浏览器网络选项卡,查看是否报告任何错误。另外,利用一些工具,例如curl带有-v选项以下载文件,它将打印有关您发布的下载请求的所有信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文