Search Posts

Corrupt zip file downloaded in angular

Angular client code:

         $http.post('/zip', {
            id: _id
        })
        .success(function (data, status, headers, config) {
            var blob = new Blob([data], {type: "application/zip"});
            var contentDisp = headers('content-disposition');
            if (contentDisp && /^attachment/i.test(contentDisp)) {
                var fileName = contentDisp.toLowerCase()
                    .split('filename=')[1]
                    .split(';')[0]
                    .replace(/"/g, '');
                //The below command works but generates a corrupt zip file.
                FileSaver.saveAs(blob, fileName);
            }
        })
        .error(function () {
            console.log("Could not download");
        });

NodeJS Server Code:

        app.route('/zip/')
        .post(function(req, res) {

          var output = fs.createWriteStream(join(outdir, outzipfile));
          //Using s3zip to archive.
          s3Zip
           .archive({ s3: s3Client, bucket: bucket}, folder, s3_files)
           .pipe(output);

          output.on('close', function() {
             //This sends back a zip file.
             res.download(outPutDirectory + outputBcemFile);
          });

          output.on('error', function(err) {
             console.log(err);
             return res.status(500).json({error: "Internal Server Error"});
          });

        });

Although FileSaver.saveAs works and downloads the zip file, it seems to be corrupted. Is the type “application/zip” correct? I have also tried “octet/stream” and it downloads a corrupt zip file too. Any help would be highly invaluable! Thanks.

Source: AngularJS

1 comment on Corrupt zip file downloaded in angular

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.