I'm using the express module in a NodeJS server to generate a zip file. The express server is responding to many requests, so I know that is set up correctly, but I'm having trouble generating a zip file and sending that back as a downloadable.
I don't want to save the file and then tell Express to send that file as a download, I just want to send the zip file as data from memory. Here is what I have so far.
function buildZipFile(data, filename) {
var zip = new require('node-zip')();
zip.file(filename, data, { base64: false });
return zip.generate();
}
var data = buildZipFile('hello world', 'hello.txt');
res.set('Content-Type', 'application/zip')
res.set('Content-Disposition', 'attachment; filename=file.zip');
res.set('Content-Length', data.length);
res.end(data, 'binary');
return;
The file will return, but neither windows unzip or 7zip are able to open the archive, as if it is corrupt. Any suggestions? Thank you in advance.