Home > database >  Big size file download
Big size file download

Time:04-29

On my web page I have the following JS code to download file from server on user click:

var oReq = new XMLHttpRequest();
oReq.open("POST", otherUrl, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(oEvent) {
    var blob = new Blob([oReq.response], 
                        {type: oReq.getResponseHeader('Content-Type')});
    var objectURL = URL.createObjectURL(blob);
    downloadLink.href = objectURL;
    downloadLink.click();
};
oReq.onprogress = function(oEvent) {
    var percentage = Math.round(oEvent.loaded/oEvent.total*100);
    
    downloadProgressDiv.innerHTML = "Progress: "   percentage   " % ("   oEvent.loaded 
                                      " from "   oEvent.total   " )";
};
oReq.send(this.path);

It works for files below 1GB size. The issue I came across happens when file size I'm trying to download is about 3GB. In this case onprogress method works as with other files, onload method is invoked once download is completed, but oReq.response=null. Also I'm able to save the file, but its size is 1KB.

Could you, please, give me any tips why does this happens? Any alternative ways of doing this?

CodePudding user response:

any tips why does this happens?

Probably because the browser runs out of memory, when you ask it to create an object URL for an object of such size.

Any alternative ways of doing this?

Don't use AJAX, but instead submit a form with method="post" to send whatever parameters you need for this.

  • Related