Tutorial

Upload Progress Reporting

Report upload progress with XMLHttpRequest upload.onprogress or fetch with a streaming response, and render a percent without re-rendering on every byte.

On this page

  1. Introduction
  2. Use XHR for upload progress
  3. Throttle updates
  4. Estimate time remaining
  5. Handle stalls
  6. Test with a slow shape
  7. Sources

Introduction

This guide covers upload progress reporting, a recurring concern in the file-upload scenario. A long upload with no feedback looks frozen, and users will close the tab. Reporting progress as a percent, throttled so you do not re-render on every byte, turns a blind wait into a visible one and cuts support tickets.

Use XHR for upload progress

fetch cannot report upload progress yet, so use XMLHttpRequest and its upload.onprogress event, which gives loaded and total on the way up.

The trade-off worth naming is that fetch cannot report upload progress yet, so use xmlhttprequest and its upload.onprogress event, which gives loaded and total on the way up adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const xhr = new XMLHttpRequest();
xhr.upload.onprogress = (e) => {
  if (e.lengthComputable) {
    const pct = Math.round((e.loaded / e.total) * 100);
    render(pct);
  }
};
xhr.open('POST', '/upload');
xhr.send(formData);

Throttle updates

Coalesce progress events to a few per second with requestAnimationFrame or a throttle, so a fast upload does not spend more time rendering than sending.

The trade-off worth naming is that coalesce progress events to a few per second with requestanimationframe or a throttle, so a fast upload does not spend more time rendering than sending adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

let last = 0;
xhr.upload.onprogress = (e) => {
  const now = Date.now();
  if (now - last < 250) return; // throttle to ~4 updates/sec
  last = now;
  if (e.lengthComputable) render(Math.round((e.loaded / e.total) * 100));
};

Estimate time remaining

Track elapsed time and bytes sent to estimate a rough ETA, and label it as approximate so a fluctuating connection does not make the number jump erratically.

The trade-off worth naming is that track elapsed time and bytes sent to estimate a rough eta, and label it as approximate so a fluctuating connection does not make the number jump erratically adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Handle stalls

If no progress for several seconds, show a stalled hint rather than a frozen bar, so the user knows the connection may be dead and can retry.

The trade-off worth naming is that if no progress for several seconds, show a stalled hint rather than a frozen bar, so the user knows the connection may be dead and can retry adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test with a slow shape

Use the devtools slow 3G preset and assert the bar moves in visible increments and reaches 100, so the progress path is exercised under realistic latency.

The trade-off worth naming is that use the devtools slow 3g preset and assert the bar moves in visible increments and reaches 100, so the progress path is exercised under realistic latency adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: XMLHttpRequest progress, fetch streaming body.

FAQ

Can I get upload progress with fetch?
Not directly; fetch does not expose upload progress yet, so use XMLHttpRequest for the upload or stream the request body through a ReadableStream with a tee that counts bytes.
How often should I update the bar?
Throttle to about 4 updates per second; more and you waste render cycles, fewer and the bar looks jerky on a fast connection.

Related Examples