Shiva
Shiva

Reputation: 51

How to download the Zip file from express js

I have three files, I have to zip and send, currently I am using archiver to zip them. I am trying to download the .zip file from express js. It prefectly works for text files, apparently it works for zip files even. but I can't see any data in the zip file after I download, it will be just like an empty text file

res.status(200).sendFile('./ABC.zip');

and it is a get request. tried setting

res.set('Content-Disposition', 'attachment; filename=file.zip');
res.set('Content-Type', 'application/zip');

But didn't help. do I need to set few more parameters for response object..?? please help

Upvotes: 1

Views: 1859

Answers (1)

Karl Philipp Sy Fabre
Karl Philipp Sy Fabre

Reputation: 155

const downloadName = `${Date.now()}.zip`;
const data = zip.toBuffer();
res.set('Content-Type','application/octet-stream');
res.set('Content-Disposition',`attachment; filename=${downloadName}`);
res.set('Content-Length',data.length);
res.send(data);

Upvotes: 1

Related Questions