English 中文(简体)
• 如何下载大宗 file本
原标题:How to download large file with JavaScript

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

正常的链接下载对我来说是徒劳的,因为我需要向请求的负责人寄出一个“灯塔”。

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.

我的“孤独”是指:

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

这里是我与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();

问题是,我如何下载更多的文件,然后下载现有的RAM-Memory,同时打上头盔?

问题回答

正如在PerSaver.js(下文链接)中发现的,你可以与各个阶层合作解决这一问题。

页: 1 (争端:我不是该回历的所有人)。 欲解决你想要解决的问题,但不能相互交叉。 目前,这只得到“ Chrome+52”和“OM+39”的支持。

或者,有FileSaver.js 。 (Disapper:我不是该邮袋的所有人),但你目前遇到同样的问题。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签