Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

javascript - Download large size files with angular file saver

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

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:

services

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

controller function

$scope.download = function (obj) {
        lovServices.attachment(obj)
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

For more information, see MDN XHR API ResponseType.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...