[英]Not able to download valid zip file using angularjs, however on directly hitting my rest api it is working
Below is my angular.js code 下面是我的angular.js代码
App.factory('JobMigrationService', ['$http','$q',function($http, $q) {
return {
downloadSvc: function(requestPayload) {
console.info(requestPayload);
return $http.get('http://localhost:8080/XXXX',{params:{"params":value}},{responseType:'arraybuffer'})
.then(
function(response){
console.info(response);
var a = document.createElement('a');
var blob = new Blob([success], {'type':"application/octet-stream",'responseType':"arraybuffer"});
a.href = URL.createObjectURL(blob);
a.download = "filename.zip";
a.click();
},
function(errResponse){
console.error('Error');
return $q.reject(errResponse);
}
);
}
}
}]);
Download is happening but when try to open the zip file it says it is invalid however using the rest api direct hit it is working fine. 下载正在发生但是当试图打开zip文件时它说它无效但是使用其余的api直接命中它工作正常。
Thanks in advance 提前致谢
I think this must be it 我认为一定是这样
var blob = new Blob([success], {'type':"application/octet-stream",'responseType':"arraybuffer"});
Whats [success] ? 什么是[成功]? shouldnt it be
不应该
var blob = new Blob(response.data, {'type':"application/octet-stream",'responseType':"arraybuffer"});
This is a bit cleaner. 这有点清洁。 I think
我认为
App.factory('JobMigrationService', ['$http','$q',function($http, $q) {
return {
downloadSvc: function(requestPayload) {
console.info(requestPayload);
return $http.get('http://localhost:8080/XXXX',{params:{"params":value}},{responseType:'blob'})
.then(
function(response){
console.info(response);
var a = document.createElement('a');
a.href = URL.createObjectURL(response.data);
a.download = "filename.zip";
a.click();
},
function(errResponse){
console.error('Error');
return $q.reject(errResponse);
}
);
}
}
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.