2017-03-01 82 views
2

我已将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) 
    } 

回答

4

而是下载到内存并将其转换成团块的。设置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

+0

是的,感谢解决方案和提供的链接。 – antonyboom

+0

由于这是一个AJAX请求,因此在调用saveAs()之前,必须将完整的文件内容加载到浏览器的内存中,对吗?那么这意味着非常大的文件刷新页面将会丢失所有文件内容? – losmescaleros

+0

[Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob)是一个类似于流的异步异步对象。 [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)是一个内存对象。通过将[XHR responseType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType)设置为Blob,可以避免将其加载到JavaScript内存的步骤。 JavaScript在加载之前将该流传递给FileSaver。 – georgeawg