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)?
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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With