2

I have a rest service made with node.js and express.js, that returns a zip trough response using express-zip:

res.zip([{path:'file', name: 'file'}]);

When i call it with node, to test it, with the following code, it creates a valid zip file:

var fs = require('fs');
var request = require('request');

request({
    url: "http://deti-lei-2.ua.pt:3500/runProgram/posio/211/1",
    method: "GET",
    encoding: null,
    headers: {
        Authorization: 'token'
    }

}).pipe(fs.createWriteStream('file.zip')).on('close', function () {
    console.log('File written!');
});

But i need to use the service with angular 2, however, i tried many ways of doing the request and none worked, i always end up with a corrupt zip. One of the ways i tried was the following:

downloadFile(program: string) {
    var bytes = [];
    for(var i = 0 ; i < program.length; i++){
        bytes.push(program.charCodeAt(i));
    }
        var FileSaver = require('file-saver');
        var blob = new Blob([bytes], {type:'application/zip'});
        FileSaver.saveAs(blob,'Program');
  }

I also tried creating a function in js with the code above, that was working, but i had no success because i could't use fs and request modules. I appreciate if someone can help.

0 Answers0