使用角度文件保护程序下载大文件 [英] Download large size files with angular file saver

查看:22
本文介绍了使用角度文件保护程序下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的项目中实现了 angular file saver 目的是下载文件和它适用于小文件,但对于大于 50mb 的文件,我看到下一个错误,下载在 35-50mb 后停止.

I have implemented angular file saver into my project with purpose to download files and it works fine for small files, but for files larger then 50mb I see next error and downloading stops after 35-50mb.

net::ERR_INCOMPLETE_CHUNKED_ENCODING

我试图在互联网上调查这个问题,发现下载有 500mb 的限制,因为显然无法在 RAM 中存储这么多信息.不幸的是,我没有找到任何其他很好的解释来解决这个问题,然后我问了后端人员,我得到的答案是他的一切都很好.

I have tried to investigate this question in internet and have found that there is a limit 500mb on downloading because obviously cannot be stored so much information in RAM. Unfortunately I didn't find any other quite explanation how to resolve this issue, then I have asked the back-end guy and I've got the answer that everything is fine on his side.

那么我的问题在哪里?我该如何解决这个问题?我感谢任何帮助

So where is my problem? and how can I resolve this issue? I appreciate any help

这是我的代码的一部分:

here is part of my code:

服务

 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)
    }

推荐答案

而不是下载到内存并转换为 blob.将 responseType 设置为 'blob':

Instead of downloading to memory and converting to blob. Set the responseType to '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.

This avoids the memory overhead of converting the stream to arraybuffer and then making a blob again.

有关更多信息,请参阅MDN XHR API ResponseType.

For more information, see MDN XHR API ResponseType.

这篇关于使用角度文件保护程序下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆