R J.
R J.

Reputation: 1542

Download multiple files from NodeJS Server

I am trying to support download of multiple files from my Node js server to the client. With the below logic implemented in my NodeJS server, I can support sending of single file to the client.

res.download('group_documents/sample1.pdf','sample1.pdf', function(err){
  if (err) {
    console.log(err);
  } else {
    // decrement a download credit etc
  }
});

How can i rewrite the above server logic to support downloading multiple files using single request from the client (browser or curl request)?

Upvotes: 3

Views: 21134

Answers (2)

atul
atul

Reputation: 600

On web browser you can not download multiple files at a time. But you can download archived file of all required files. Using express-zip npm package you can do it easily.

https://github.com/thrackle/express-zip

Install express-zip.

npm install express-zip

Use following code :

res.zip(files);

files is an array of objects. Each object should be :

{ path : 'path/to/file', name : 'fileName'}

Upvotes: 9

paul
paul

Reputation: 22021

if your target clients are web browsers, then you can't, as the http protocol does not allow for multiple downloads for a single request.

If you really need to do this, then you'd need to either write a custom client application, or (more simply) zip the files into some archive before transmitting them.

Upvotes: 0

Related Questions