[英]angularjs download corrupted zip file
in my angular application I need to download a zip file, the download starts regularly, but the client download a corrupted zip file, whose size is only 15 byte. 在我的角度应用程序中,我需要下载一个zip文件,下载会定期开始,但是客户端会下载一个损坏的zip文件,该文件的大小仅为15个字节。 this is the code for the request:
这是请求的代码:
$http.post('/api/download/',data, {
dataType : "binary",
processData : false,
accept:'application/zip',
Encoding: 'gzip',
responseType:'arraybuffer'})
this is how I handle the successful response: 这是我处理成功响应的方式:
function() {
var data = {}
data.files = $scope.selectedFiles
FileService.download(data,function(resp){
var blob = new Blob([resp], {type: "application/octet-stream"})
console.log('response',resp)
FileSaver.saveAs(blob,'registrazioni.zip',(err) => {
console.log('saved success,error',err);
})
})
}
I read many similar questions, but theirs solutions do not apply to me 我读过许多类似的问题,但是他们的解决方案不适用于我
尝试在Github中的FileSaver上检查此错误: 保存Zip文件问题
First of all you need to set the responseType to arraybuffer.See sending_and_Receiving_Binary_Data. 首先,您需要将responseType设置为arraybuffer。请参见sending_and_Receiving_Binary_Data。 So your code will like this:
因此,您的代码将如下所示:
$http.post('/postUrlHere',{myParams}, {responseType:'arraybuffer'})
.success(function (response) {
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
});
In Controller: 在控制器中:
$scope.cont = $sce.trustAsResourceUrl(fileURL);
In html Page: 在html页面中:
<embed ng-src="{{cont}}" style="width:100px;height:100px;"></embed>
This code Will be help you to solve problem. 此代码将帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.