Node.js - 将多个文件作为附件从服务器推送到客户端

标签 node.js express xmlhttprequest ssh2-sftp

我有一个应用程序,它允许用户在文本输入框中输入多个文件名,并在提交时从 SFTP 服务器获取这些文件名并返回给客户端并下载。

应用看起来像这样:

Application

POST 请求的代码如下所示:

// Declare variables.
    var files = req.body.input;
    var i = 0;

    // Check if request body is an array or single value.
    if ( Array.isArray(files) ) {

        // Loop through the array of file names.
        function myLoop() {
            setTimeout(function() {

                // Declare the files remote and local paths as variables.
                var remoteFilename = '../mnt/volume_lon1_01/test/files/processed/' + files[i] + '.csv.gz';
                var localFilename = files[i] + '.csv.gz'

                // Use the SFTP Get command to get the files.
                sftp.get(remoteFilename).then((stream) => {

                    // Pass the file back to the client side for download.
                    res.set('content-disposition', `attachment; filename="${ localFilename }"`);
                    stream.pipe(res);
                });

                // Increment the counter.
                i++;

            }, 200)
        }
        myLoop();

    } else {

        // If the request body is a single value, declare the files remote and local path as a variable.
        var remoteFilename = '../mnt/volume_lon1_01/test/files/processed/' + files + '.csv.gz';
        var localFilename = files[i] + '.csv.gz'

        // Use the SFTP Get command to get the files.
        sftp.get(remoteFilename).then((stream) => {

            // Pass the file back to the client side for download.
            res.set('content-disposition', `attachment; filename="${ localFilename }"`);
            stream.pipe(res);
        });
    }

})

我的问题是:如何从服务器端代码向客户端发送多个文件以供下载?

我在这里看到这个问题:Sending multiple files down the pipe但给出的答案并没有真正详细说明解决方案。我知道我的代码永远不会适用于多个文件,我只是附上它作为展示我到目前为止所拥有的东西的一种方式。它适用于下载 1 个文件,但仅此而已,因为根据我对服务器知识有限的了解, header 只发送一次,因此我无法循环设置文件名并一个一个地发送它们。

Mscdex 在我链接到的问题中的回答解释了:

There is no way to send multiple files like that in a single response, unless you use your own special formatting (standard multipart or otherwise) and then parse that on the client side (e.g. via XHR).

有人可以解释并演示“使用您自己的特殊格式意味着什么”吗,老实说,我一点头绪都没有。

此外,我想尽可能避免压缩文件。

非常感谢, G

最佳答案

“使用您自己的特殊格式” 是一个可能 可行的解决方案,但它是一个非标准解决方案,还需要自定义客户端(浏览器) 代码来完成它。我会反对它,并使用下一个最好的方法:创建一个 ZIP 文件。

或者,在客户端代码中,您可以包装每个 <input>在自己的<form> ,并在按下提交按钮时,使用一些 JS 代码来提交所有这些表单。在这种情况下,每个表单都会触发一次下载,因此服务器端代码就是 else。 block 。

关于Node.js - 将多个文件作为附件从服务器推送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409573/

相关文章:

php - HTTP 和 NodeJS 位于不同的服务器上

javascript - res.statusCode 给出未定义

javascript - 为什么这个字符串不能解析为 JSON?

javascript - 根据 MySQL 数据库值动态更改网站内容

javascript - 为什么 {} == {} 为假,但 {} + {} == {} + {} 为真

javascript - 有没有办法在 typescript 上使用 util.promisify 继承方法签名?

javascript - 使用 Node 运行脚本的计划任务将不起作用

javascript - ExpressJS 在 html5 模式上进行更深层次的 url 链接时呈现错误的静态路径

node.js - sequelize belongsToMany 给出 TypeError : undefined is not a function

Android 上具有 Keep-Alive 的 JavaScript XmlHttpRequest