0

Recently I posted an answer https://stackoverflow.com/a/74098768/2026065 to the following question here Pause a download with XMLHttpRequest in Javascript

But this actually raised a question in my mind where is the Javascript holding partially downloaded data until it is not completely downloaded? I tried going through the xhr object of XMLHttpRequest and its events but nowhere I could find the partially downloaded data which was downloaded in chunks from Server. A Little Code like

let xhr = new XMLHttpRequest();
xhr.open('get', someURLtoJPG, true);
xhr.responseType = 'blob';
xhr.setRequestHeader('Range', `bytes=0-`); //<--No Ending Bytes so file should be downloaded complete in single request
console.log(xhr); //<--No property for partially downloaded data is updated here
xhr.onprogress = function(event){
    console.log(event.loaded); //<--Shows how much of partial data is downloaded
    console.log(event); //<--No Partially Downloaded Data here
    console.log(this.response); //<--No Partially Downloaded Data here
    console.log(xhr); //<--No property for partially downloaded data is updated here
}
xhr.onload = function(e){
    console.log(this.response); //<--Data only available here when it is completely downloaded
}
xhr.send();

If we abort the xhr request or some error is caused in the middle, all of the partially downloaded data is wiped.

I also enabled Caching File from server and tried aborting the request in the middle with xhr.abort() and then calling the same someURLtoJPG with new xhr request so, that it might have downloaded the data in Browser Cache but no it wasn't there and it started downloading the file from beginning.

So, the actual question is How to get the Partially Downloaded Data?

Javascript gives us great APIs and tools and it's just Awesome so, is there any possibility to get this Partially Downloaded Data even before it is completely downloaded? If there is a chance to do so, then we might be able to Pause & Resume the XMLHttpRequest easily.

Out of curiosity, I opened web.whatsapp.com and checked what they are doing and as expected they are also downloading the file from beginning if we cancel the download in the middle (both xhr/fetch) while whatsapp app which is off-course native app allows us to resume the file download if we cancel in the middle.

Is it even possible with Fetch?

Airy
  • 5,484
  • 7
  • 53
  • 78

0 Answers0