How to trigger Zip download on server?


#1

On my server I am storing a base64 data url of a zip folder. When someone hits a certain server side route, I want to trigger a download of the zip file. Previously, I was using window.open("data:application/zip;base64, " + data_url); client side to get it to work. Now I’m trying to get the download working when someone hits a particular server side route… How would I do that? I have been trying this:

Router.route('/zd/:_id', function () {
    var req = this.request;
    var res = this.response;
    
    var zipDownload = ZipDownloads.findOne(this.params._id);

    if (zipDownload) {
        res.writeHead(200, {'Content-Type': 'application/octet-stream',"Content-Disposition": "attachment; filename=download.zip"});
        res.end(zipDownload.zip_data);
    } else {
        res.end("This link is no longer active");

    }


}, {where: 'server'});

However, the zip that it returns is corrupted and turns into a .zip.cpgz. I’d appreciate any help!


#2

Maybe you need to specify the encoding when calling res.end()? Check out this SO post