简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM