javascript - 让 Node.js 将缓冲区作为文件发送

标签 javascript node.js excel nginx download

我正在尝试在服务器端创建一个 Excel 工作簿,当用户访问某个路线时(使用express.js)发送给用户 我正在使用 https://github.com/riyadhalnur/excelbuilderjs-node 中的模块 并且我在 workBookB64 常量中确实有大量数据,并且在我的浏览器(chrome)中,我确实在 res.end() 调用之后获得了处置和内容类型的 header 。 但是该文件永远不会下载到浏览器端。 我的 Node 进程在 Nginx 反向代理后面运行,以处理 SSL。 任何有关如何下载文件的想法都很棒!

    const result      = Excel.createFile( workBook );
    const workBookB64 = new Buffer( result, 'base64' );

    res.setHeader( 'Content-Disposition', 'attachment; filename=' + fileName );
    res.setHeader( 'Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64' );
    res.end( workBookB64 );

最佳答案

我自己解决了这个问题。事实证明,问题出在我请求 URL 的方式上。客户端代码使用 jQuery 调用: $.get("/getReport") 因此响应将返回到此调用,该调用没有回调处理程序。所以对我来说,它似乎不起作用。 我使用的解决方案是去掉JS,只使用 anchor 标记,直接调用URL。

关于javascript - 让 Node.js 将缓冲区作为文件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39521751/

相关文章:

javascript - Php 和 Html 图表使用 Canvas.js 使用 mysql 数据库

Javascript 悬停不起作用

excel - 对 n 个随机数求和(用于模拟目的)

javascript - JS : How to redirect an user to another page when user refresh the page?

javascript - 为什么 Object.getOwnPropertyDescriptor({}, '__proto__' ) 返回未定义?

node.js - Node.js 中的 LRU 缓存

javascript - 如何使用nodejs找出文件重命名事件?

javascript - 如何使用 Node js从mysql数据库中获取值

python - 属性错误 : 'Sheet' object has no attribute 'write'

excel - 无法在Excel vba宏中指定确切的范围