how to download a zip file

后端 未结 3 1179
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 04:00

I am trying to download a zip file from my web api controller. It is returning the file but I am getting a message the zipfile is invalid when i try to open. I have seen oth

相关标签:
3条回答
  • 2020-12-09 04:28

    At a matter of fact you are rigth adding responseType:'arraybuffer'. That added to the following code when received the response from ajax will prompt a file to download:

    var a = document.createElement('a');
    var blob = new Blob([responseData], {'type':"application/octet-stream"});
    a.href = URL.createObjectURL(blob);
    a.download = "filename.zip";
    a.click();
    
    0 讨论(0)
  • 2020-12-09 04:35

    Below code is working fine for me for download zip file,

    Controller

    $scope.downloadExport = function (id,filename,frmdata) {
             frmdata = {};
            var data = tdioServices.downloadexport(id,filename,frmdata);
             data.success(function(success) {
                    var blob = new Blob([success], { type:"arraybuffer" });           
                    var downloadLink = angular.element('<a></a>');
                    downloadLink.attr('href',window.URL.createObjectURL(blob));
                    downloadLink.attr('download', filename);
                    downloadLink[0].click();
                    $COMMON_ACCEPT = "Download Successfully";
                    $COMMON_ACCEPT_TEXT = "Success";
                    toastr.success($COMMON_ACCEPT, $COMMON_ACCEPT_TEXT);
    
            });
        };
    

    Services

     this.downloadexport = function(id,filename,frmdata){
           var request = $http({method:'get', url:APP_URL+'/exports/'+id+'/download/'+filename, data:frmdata,responseType:'arraybuffer'});
           return request;        
        }
    
    0 讨论(0)
  • 2020-12-09 04:38

    I think you're setting the responseType in the wrong place, instead of this:

    $http.post('/api/apiZipPipeLine/', model)
    

    Try this:

    $http.post('/api/apiZipPipeLine/', model, {responseType:'arraybuffer'})
    

    Take a look at this answer for more details.

    0 讨论(0)
提交回复
热议问题