最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xmlhttprequest - How to download large file with JavaScript - Stack Overflow

programmeradmin2浏览0评论

I need to download a large file with JavaScript using XMLHttpRequest or fetch without saving the file first in the RAM-Memory.

Normal link download doesn't work for me, because I need to send a Bearer Token in the header of the request.

I could manage to download a file, but this "solution", it's saving the file first in the RAM-Memory, before I get a save dialog, so that the Browser will brake if the file is larger then the available RAM-Memory.

Here is my "solution" with fetch:

        var myHeaders = new Headers();
        myHeaders.append('Authorization', `Bearer ${token}`);

        var myInit = { method: 'GET',
            headers: myHeaders,
            mode: 'cors',
            cache: 'default' };
        var a = document.createElement('a');

        fetch(url,myInit)
            .then((response)=> {
                return response.blob();
            })
            .then((myBlob)=> {
                a.href = window.URL.createObjectURL(myBlob);
                var attr = document.createAttribute("download");
                a.setAttributeNode(attr);
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
                a.remove();
            });

And here is my "solution" with XMLHttpRequest:

        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = ()=>{
            if (xhttp.readyState == 4){
                if ((xhttp.status == 200) || (xhttp.status == 0)){
                    var a = document.createElement('a');
                    a.href = window.URL.createObjectURL(xhttp.response); // xhr.response is a blob
                    var attr = document.createAttribute("download");
                    a.setAttributeNode(attr);
                    a.style.display = 'none';
                    document.body.appendChild(a);
                    a.click();
                    a.remove();
                }
            }
        };
        xhttp.open("GET", url);
        xhttp.responseType = "blob";
        xhttp.setRequestHeader('Authorization', `Bearer ${token}`);
        xhttp.send();

The question is how can I download files larger then the available RAM-Memory and in the same time setting the headers?

I need to download a large file with JavaScript using XMLHttpRequest or fetch without saving the file first in the RAM-Memory.

Normal link download doesn't work for me, because I need to send a Bearer Token in the header of the request.

I could manage to download a file, but this "solution", it's saving the file first in the RAM-Memory, before I get a save dialog, so that the Browser will brake if the file is larger then the available RAM-Memory.

Here is my "solution" with fetch:

        var myHeaders = new Headers();
        myHeaders.append('Authorization', `Bearer ${token}`);

        var myInit = { method: 'GET',
            headers: myHeaders,
            mode: 'cors',
            cache: 'default' };
        var a = document.createElement('a');

        fetch(url,myInit)
            .then((response)=> {
                return response.blob();
            })
            .then((myBlob)=> {
                a.href = window.URL.createObjectURL(myBlob);
                var attr = document.createAttribute("download");
                a.setAttributeNode(attr);
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
                a.remove();
            });

And here is my "solution" with XMLHttpRequest:

        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = ()=>{
            if (xhttp.readyState == 4){
                if ((xhttp.status == 200) || (xhttp.status == 0)){
                    var a = document.createElement('a');
                    a.href = window.URL.createObjectURL(xhttp.response); // xhr.response is a blob
                    var attr = document.createAttribute("download");
                    a.setAttributeNode(attr);
                    a.style.display = 'none';
                    document.body.appendChild(a);
                    a.click();
                    a.remove();
                }
            }
        };
        xhttp.open("GET", url);
        xhttp.responseType = "blob";
        xhttp.setRequestHeader('Authorization', `Bearer ${token}`);
        xhttp.send();

The question is how can I download files larger then the available RAM-Memory and in the same time setting the headers?

Share Improve this question edited Oct 25, 2017 at 14:42 Sheki asked Oct 25, 2017 at 12:16 ShekiSheki 1,6351 gold badge17 silver badges25 bronze badges 2
  • you can find the answer here: stackoverflow./questions/4545311/… – juanecabellob Commented Oct 25, 2017 at 12:29
  • @juancab jQuery file download seams to be not the solution, I can't even send my headers (Bearer Token), if I'm not wrong, jQuery. file download seams to be just a solution for that what I already solved and that's getting a save dialog. I din't tested but I think jQuery file download will have the same problem like I have, and that is downloading larger files than the RAM-memory, pleas correct me if I'm wrong. – Sheki Commented Oct 25, 2017 at 13:07
Add a ment  | 

1 Answer 1

Reset to default -1

As found in StreamSaver.js (link below), you could work with streams to workaround this issue.

You can try StreamSaver.js (Disclaimer: I am not the owner of that repo). Seems to solve what you want to the extent that it is not cross-browser patible. Currently it is only supported by Chrome +52 and Opera +39.

Alternatively, there is FileSaver.js (Disclaimer: I am not the owner of that repo) but you'd run into the same problems you are currently running into.

发布评论

评论列表(0)

  1. 暂无评论