Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download multiple files from NodeJS Server

Tags:

node.js

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)?

like image 262
R J. Avatar asked Nov 24 '25 03:11

R J.


1 Answers

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'}
like image 126
atul Avatar answered Nov 25 '25 15:11

atul