javascript show kb left of a download

is it possible to show the user via javascript how much is left (in kb) to fully download a gif file from a remote server?

    function downgif(id, img){
    //show downloading
    Show('downloadgif');
    
    //chenge img
    document.getElementById(id).src=img;
   
   //test if complete
    var completeInterval = null, renderedInterval = null, count = 0;
    var theImg = document.getElementById(id);
    theImg.src = img;
    // Wait for image to be loaded (but not necessarily rendered)
    completeInterval = setInterval(function() {
      if (theImg.complete) {
        // Cancel checking once IMG is loaded OR we've tried for ~9s already
        clearInterval(completeInterval);
        completeInterval = null;
          // IMG is now 'complete' - but that just means it's in the render queue...
          // Wait for naturalWidth and naturalHeight to be > 0 since this shows
          // that the image is done being RENDERED (not just 'loaded/complete')
          renderedInterval = setInterval(function() {
            if (theImg.naturalHeight > 0 && theImg.naturalWidth > 0) {
              clearInterval(renderedInterval);
              renderedInterval = null;

              //hide downliading
              Hide('downloadgif');
            }
          }, 100);
      }
    }, 450);
 
}

Yes, it’s possible to display how much is left to fully download a GIF file from a remote server, but it requires using the XMLHttpRequest or Fetch API to track download progress (rather than a <img />)

XMLHttpRequest

XMLHttpRequest provides a onprogress event which can be used to calculate the progress of a download.

function downgif(url, imgElementId) {
    const xhr = new XMLHttpRequest();
    const imgElement = document.getElementById(imgElementId);

    xhr.open('GET', url, true);
    xhr.responseType = 'blob';

    xhr.onprogress = function(event) {
          const kbRemaining = ((event.total - event.loaded) / 1024).toFixed(2);
          console.log(`Remaining: ${kbRemaining} KB`);
    };

    xhr.onload = function() {
          const blobUrl = URL.createObjectURL(xhr.response);
          imgElement.src = blobUrl;
          console.log("Download complete.");
    };

    xhr.send();
}

const url = "https://upload.wikimedia.org/wikipedia/commons/3/31/-WikiForHumanRights2022Right_to_a_healthy_environment._%281%29_%281%29.gif";
downgif(url, 'gifDisplay');
<img id="gifDisplay">

Fetch

fetch allows you to download the response as a stream, which can also have it’s progress calculated from the Content-Length header.

async function downgif(url, imgElementId) {
    const imgElement = document.getElementById(imgElementId);
    const response = await fetch(url);
    
    const contentLength = response.headers.get('Content-Length');
    
    const total = parseInt(contentLength, 10);
    let loaded = 0;

    const reader = response.body.getReader();
    const chunks = [];
    while (true) {
        const { done, value } = await reader.read();
        if (done) break;

        chunks.push(value);
        loaded += value.length;

        const kbRemaining = ((total - loaded) / 1024).toFixed(2);

        console.log(`Remaining: ${kbRemaining} KB`);
    }


    const blob = new Blob(chunks);
    const blobUrl = URL.createObjectURL(blob);
    imgElement.src = blobUrl;

    console.log("Download complete.");
}

const url = "https://upload.wikimedia.org/wikipedia/commons/3/31/-WikiForHumanRights2022Right_to_a_healthy_environment._%281%29_%281%29.gif";
downgif(url, 'gifDisplay');
<img id="gifDisplay">

If you want to test this on other large GIFs, Wikipedia provides many examples (which is where the demo GIF I used came from)

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật