Yasiru Nilan
Yasiru Nilan

Reputation: 457

How to download multiple files using ExpressJS one API call

I'm quite new to expressjs and I'm developing a web application which acts as an API application. There is a react frontend application also. When a button is clicked in the client app it will send an API call to the backend app and will download a file. That scenario is working fine.with the following code.

  const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;
  res.download(file); // Set disposition and send it.
});

But now I have a requirement to download multiple files from a button click. How can I do that . Can someone help me here.

Upvotes: 1

Views: 1922

Answers (1)

apsillers
apsillers

Reputation: 115910

An HTTP response can only have one file. Really, "downloading a file" in HTTP means serving a response with a Content-Disposition: attachment header, to hint to the client that this response should be saved to the filesystem instead of rendered in the browser.

To download multiple files, you want the client code to initiate multiple HTTP requests (probably to different URLs), and the server can respond to each request with a different file. Note that many browsers will refuse to download multiple files in response to a single user action (for fear of flooding the user with unwanted files) or will at least prompt for confirmation before doing so.

If you cannot change your client-side code to make multiple requests, you will need to package your files inside a single file archive.

Upvotes: 2

Related Questions