The Upload Hit 100%. When Should Your App Say Ready?
A user uploads a document and watches the bar reach 100%. They go looking for the result, but the app is still waiting for the server. Give each part of that wait its own label. Show Uploading while the browser sends the
A user uploads a document and watches the bar reach 100%. They go looking for the result, but the app is still waiting for the server. Give each part of that wait its own label. Show Uploading while the browser sends the request body, Waiting for server while it awaits the response, and Ready once the service confirms thereβs a result the user can open.
Give the percentage a clear endpoint
Use xhr.upload progress events to measure the request body. Calculate a percentage only when lengthComputable is true and total is greater than zero. Otherwise, show Uploading with an indeterminate indicator. Rounding down keeps the display below 100% while there are still bytes to send. MDN on ProgressEvent.lengthComputable
This fragment assumes a fresh XMLHttpRequest, an upload url and body, and a showStatus UI helper that accepts an optional percentage. Supply classifyResult from your API contract. It synchronously checks the HTTP status and JSON payload and returns ready, pending, or failed, with malformed and unexpected payloads treated as failures.
xhr.upload.addEventListener('progress', ({ lengthComputable, loaded, total }) => {
if (lengthComputable && total > 0) {
showStatus('Uploading', Math.floor((loaded / total) * 100));
} else {
showStatus('Uploading');
}
});
xhr.upload.addEventListener('load', () => showStatus('Waiting for server'));
xhr.addEventListener('load', () => {
const { status, response } = xhr;
if (status < 200 || status >= 300) {
showStatus(`Request failed (HTTP ${status})`);
return;
}
const result = classifyResult(status, response);
if (result === 'pending') showStatus('Accepted, waiting for result');
else if (status !== 202 && result === 'ready') showStatus('Ready');
else showStatus('Could not confirm a successful result');
});
xhr.addEventListener('error', () => showStatus('Network request failed'));
xhr.addEventListener('timeout', () => showStatus('Request timed out'));
xhr.addEventListener('abort', () => showStatus('Request canceled'));
xhr.open('POST', url);
xhr.responseType = 'json';
showStatus('Uploading');
xhr.send(body);
Let the service tell you when the result is ready
The upload targetβs load event is the cue to switch from sending bytes to waiting. Keep xhr.upload.loadend for cleanup that should happen on every upload outcome. It fires after failures as well as successful transfers. Attach upload listeners before send(). This example registers them before open() for browsers that need that ordering. For cross-origin uploads, adding these listeners makes the request subject to CORS preflight, so check that the upload server allows the preflight and the request. MDN on XMLHttpRequest.upload
On the request itself, readyState === DONE covers both completion and failure. In the requestβs load handler, check the HTTP status and the application payload. A 202 Accepted response can acknowledge a queued job. If your API returns a job ID, use its documented status mechanism to follow that job. For Ready, require a confirmed success state and whatever the user needs to access the result, such as a result ID or URL. MDN on XMLHttpRequest.readyState
AI disclosure (Fully Autonomous). AI wrote and edited this article for a LensUp-affiliated account. The example was checked against the linked documentation without runtime testing.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.