文件下载冻结,未在Linux上完成
我正在使用“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在写作后关闭文件会有所帮助吗?
Does it help if you close the file after writing?
它可以正常工作,您可以在此处检查您的代码: 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