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
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();
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;
}
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.