Receive zip file, angularJs

后端 未结 2 1432
夕颜
夕颜 2021-01-29 09:17

I\'ve got a problem when I want to download a zip file from a Rest api,

When the zip file is tranfered from my server (with jersey), I receive it corrupted, ...

2条回答
  •  野性不改
    2021-01-29 09:37

    I encountered this problem in the past. You need to use the Buffer as well and trigger the opening of the "Save As" dialog, as described below:

    var url = (...)
    var expectedMediaType = (...)
    
    var requestData = (...)
    $http.post(url, requestData, {
      params: {
        queryParam: 'queryParamValue'
      },
      headers: {
        'Content-Type': 'application/json',
        'Accept': expectedMediaType
      }
    }).then(function (response) {
      var filename = (...)
      openSaveAsDialog(filename, response.data, expectedMediaType);
    });
    

    Here is the content of the openSaveAsDialog function:

    function openSaveAsDialog(filename, content, mediaType) {
      var blob = new Blob([content], {type: mediaType});
      saveAs(blob, filename);
    }
    

    To use the saveAs function, you need to include https://github.com/eligrey/FileSaver.js library. To install it, just reference its js file using a tag script in your HTML page.

    
    

    I wrote a blog post describing how to fix it: https://templth.wordpress.com/2014/11/21/handle-downloads-with-angular/.

    Hope it will help you, Thierry

提交回复
热议问题