我已经将angular file saver实现到我的项目中,目的是下载文件,它适用于小文件,但对于大于50mb的文件,我看到下一个错误,下载在35-50mb后停止。
net::ERR_INCOMPLETE_CHUNKED_ENCODING
我试图在互联网上调查这个问题并且发现下载时限制为500mb,因为显然无法在RAM中存储这么多信息。不幸的是,我没有找到任何其他解释如何解决这个问题,然后我问了后端人,我得到的答案是他的一切都很好。
那我的问题在哪里?以及如何解决此问题?我感谢任何帮助
这是我的代码的一部分:
服务
function attachment(obj) {
custom.responseType = "arraybuffer";
delete custom.params.limit;
delete custom.params.offset;
delete custom.params.orderBy;
delete custom.params.insertedAt;
var contentType = obj.mimeType;
var name = obj.displayFilename;
return $http.get(Config.rurl('attachments') + '/' + obj.bucketName + '/' + obj.path + '?displayFilename=' + obj.displayFilename, custom)
.then(function (response) {
var data = new Blob([response.data], { type: contentType });
FileSaver.saveAs(data, name);
delete custom.responseType
})
.catch(function (err) {
delete custom.responseType;
alert("It has happened an error. Downloading has been stopped") ;
});
}
控制器功能
$scope.download = function (obj) {
lovServices.attachment(obj)
}
答案 0 :(得分:4)
而不是下载到内存并转换为blob。将responseType
设置为'blob'
:
//SET responseType to 'blob'
var config = { responseType: ̶'̶a̶r̶r̶a̶y̶b̶u̶f̶f̶e̶r̶'̶ ̶ 'blob' };
return $http.get(url, config)
.then(function (response) {
̶v̶a̶r̶ ̶d̶a̶t̶a̶ ̶=̶ ̶n̶e̶w̶ ̶B̶l̶o̶b̶(̶[̶r̶e̶s̶p̶o̶n̶s̶e̶.̶d̶a̶t̶a̶]̶,̶ ̶{̶ ̶t̶y̶p̶e̶:̶ ̶c̶o̶n̶t̶e̶n̶t̶T̶y̶p̶e̶ ̶}̶)̶;̶
//USE blob response
var data = response.data;
FileSaver.saveAs(data, name);
})
.catch(function (err) {
alert("It has happened an error. Downloading has been stopped") ;
throw err;
});
这可以避免将流转换为arraybuffer然后再次生成blob的内存开销。
有关详细信息,请参阅MDN XHR API ResponseType。