title: Node.js - Pushing multiple files as attachments from server to client
body: <p>I have an application which allows a user to enter multiple file names into text input boxes and on submit these file names will be fetched from an SFTP server and returned back to the client and downloaded.</p>
  
  <p>The app looks something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/v829O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v829O.png" alt="Application"></a></p>
  
  <p>The code for the POST request looks like this:</p>
  
  <pre><code>// 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) =&gt; {
  
                      // 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) =&gt; {
  
              // Pass the file back to the client side for download.
              res.set('content-disposition', `attachment; filename="${ localFilename }"`);
              stream.pipe(res);
          });
      }
  
  })
  </code></pre>
  
  <p>My question is this: How is it possible to send multiple files for download to the client from this server side code?</p>
  
  <p>I've seen this question here: <a href="https://stackoverflow.com/questions/30923010/sending-multiple-files-down-the-pipe">Sending multiple files down the pipe</a> but the answer given doesn't really detail a solution. I know my code will never work for multiple files, i've just attached it as a means of demonstrating what I have thus far. It works fine for downloading 1 file but no more as, from what I understand with limited server knowledge, headers are sent once so I cannot set the file names in a loop and send them in one by one.</p>
  
  <p>Mscdex's answer in the question I have linked to explains:</p>
  
  <blockquote>
    <p>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).</p>
  </blockquote>
  
  <p>Can someone explain and perhaps demonstrate what 'Using your own special formatting means' as I honestly don't have a clue.</p>
  
  <p>Also, I would like to avoid zipping the files if at all possible.</p>
  
  <p>Many thanks in advance,
  G</p>