Client loading of the server created zip file

Before anyone says “duplicate,” I just want to make sure people know that I have already addressed these issues:

1) Uses angular and php, not sure what is going on here (I don't know PHP): Download the zip file and run the "save file" dialog from the angular method

2) Unable to get this answer: how to download a zip file using angular

3) This person can already download, that has passed the point that I am trying to find out: Download the external zip file from angular, caused by the action of the button

4) There is no answer for this: download the .zip file from the server in nodejs

5) I don’t know what language it is in general: https://stackoverflow.com/questions/35596764/zip-file-download-using-angularjs-directive

Given these questions , if this is still a duplicate, I apologize. Here is another version of this question.

My angular 1.5.X client gives me a list of names, of which each has an associated file. My Node 4.X / Express 4.X server accepts this list, gets the location of the files, creates a zip file using the express zip code from npm, and then sends this file back to the response. Then I want my client to initiate the browser setting "upload file".

Here is my client code (Angular 1.5.X):

function bulkdownload(titles){
    titles = titles || [];
    if ( titles.length > 0 ) {
        $http.get('/query/bulkdownload',{
            params:{titles:titles},
            responseType:'arraybuffer'
        })
        .then(successCb,errorCb)
        .catch(exceptionCb);
    }

    function successCb(response){
        // This is the part I believe I cannot get to work, my code snippet is below
    };

    function errorCb(error){
            alert('Error: ' + JSON.stringify(error));
    };

    function exceptionCb(ex){
            alert('Exception: ' + JSON.stringify(ex));
    };
};

Node (4.X) code with express mail, https://www.npmjs.com/package/express-zip :

router.get('/bulkdownload',function(req,resp){
    var titles = req.query.titles || [];

    if ( titles.length > 0 ){
        utils.getFileLocations(titles).
        then(function(files){
            let filename = 'zipfile.zip';

            // .zip sets Content-Type and Content-disposition
            resp.zip(files,filename,console.log);
        },
        _errorCb)
    }
});

Here is my successCb in my client code (Angular 1.5.X):

function successCb(response){
    var URL = $window.URL || $window.webkitURL || $window.mozURL || $window.msURL;
    if ( URL ) {
        var blob = new Blob([response.data],{type:'application/zip'});
        var url = URL.createObjectURL(blob);
        $window.open(url);
    }
};

"blob", , . IE, . , blob - HTML5, " " . ? , ?

90% + IE11, angular PhantomJS (Karma) IE. , " " :

Exception: {"description":"Access is denied...<stack trace>}

, , .. !

+4
4

bulkdownload $window.open(...) $http.get(...):

function bulkdownload(titles){
    titles = titles || [];
    if ( titles.length > 0 ) {
        var url = '/query/bulkdownload?';
        var len = titles.length;
        for ( var ii = 0; ii < len; ii++ ) {
            url = url + 'titles=' + titles[ii];
            if ( ii < len-1 ) {
                url = url + '&';
            }
        }
        $window.open(url);
    }
};

IE11.

0
var zip_file_path = "" //put inside "" your path with file.zip
var zip_file_name = "" //put inside "" file name or something
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = zip_file_path;
a.download = zip_file_name;
a.click();
document.body.removeChild(a);
+3

:

var url="YOUR ZIP URL HERE";
window.open(url, '_blank');
+1

, Javascript, byte[] array.

( ) blob:

var b64toBlob = function(b64Data, contentType, sliceSize) {
      contentType = contentType || '';
      sliceSize = sliceSize || 512;

      var byteCharacters = atob(b64Data);
      var byteArrays = [];

      for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);

        var byteNumbers = new Array(slice.length);
        for (var i = 0; i < slice.length; i++) {
          byteNumbers[i] = slice.charCodeAt(i);
        }

        var byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
      }

      var blob = new Blob(byteArrays, {type: contentType});
      return blob;
};

blob FileSaver.js ( Angular.js $http.get):

    $http.get("your/api/uri").success(function(data, status, headers, config) {
        //Here, data is type of string
        var blob = b64toBlob(data, 'application/zip');
        var fileName = "download.zip";
        saveAs(blob, fileName);
    });

Note. I am posting byte[] array(Java-Server-Side) as follows:

byte[] myByteArray = /*generate your zip file and convert into byte array*/ new byte[]();
return new ResponseEntity<byte[]>(myByteArray , headers, HttpStatus.OK);
0
source

All Articles