WebMriya
Back to articles

Reliable File Uploads in React: State, Retries, and Resume

A practical React upload design for progress, retries, cancellation, and resume—without letting network timing corrupt UI state or duplicate files.

React
TypeScript
Frontend Architecture
W
Frontend Architecture & Legacy Modernization
November 3, 2026 · 5 min read

A user uploads a large document, sees the progress bar reach 100%, then gets an error. They try again; the server now contains two copies, but the interface shows one. This is not just a progress-indicator bug. Uploads combine a long-running operation, unreliable network requests, and UI state that can outlive the component that started it.

A reliable implementation starts by treating an upload as a lifecycle, not a single request. The client should make each phase explicit, retain enough information to recover from failure, and avoid claiming that a file is saved until the server confirms it.

Model the lifecycle before writing the request

A boolean such as isUploading cannot describe the states an actual upload passes through. At minimum, distinguish queued, uploading, failed, cancelled, and completed. Keep the file, progress, and any error alongside that status. This follows the same principle as explicit UI states in React: represent states that can occur, rather than combinations of flags that accidentally permit contradictory screens.

  • queued: accepted locally, but no request has started;
  • uploading: a request is active and progress may be reported;
  • failed: the server did not confirm completion; the user may retry;
  • cancelled: the user stopped the operation; this is not the same as failure;
  • completed: the server confirmed the uploaded file and returned its identifier.

Keep a stable client-side ID for each selected file. Do not use the filename as the identity: users can select two files with the same name, and a retry must refer to the same upload row. The ID is for UI reconciliation; it is not a substitute for a server-side idempotency key.

Make request results safe to apply

Network responses can arrive after a user has cancelled an upload, removed its row, or started a retry. If every response blindly updates state, an old success can overwrite the new attempt. Associate each attempt with a generation number or unique attempt ID, and apply its result only if that attempt is still current.

ts
type UploadStatus = "queued" | "uploading" | "failed" | "cancelled" | "completed";

type UploadItem = {
  id: string;
  file: File;
  status: UploadStatus;
  progress: number;
  attempt: number;
  error?: string;
};

function isCurrentAttempt(item: UploadItem, attempt: number) {
  return item.attempt === attempt && item.status === "uploading";
}

On retry, increment the attempt value before starting the new request. On cancellation, update the status and abort the request. Both safeguards matter: aborting can stop unnecessary work, while checking the attempt prevents a late response from changing the visible result. For a broader discussion of validating data received from servers, see runtime validation at API boundaries.

Treat progress as a hint, not proof

Progress events usually measure bytes sent by the browser. They do not prove that the server has validated, stored, or processed the file. Keep the item in uploading state after the transfer reaches 100%, and show completion only after the server responds successfully with a file ID or equivalent confirmation. If the request fails after all bytes were sent, the transfer may have finished while the upload operation did not.

Client observationWhat it actually meansSafe UI response
Progress reaches 100%The browser reports that the request body was sent.Keep the item uploading until the server confirms success.
Request times outThe client does not know whether the server completed the operation.Retry using an idempotency key or ask the server for upload status.
User cancelsThe client has requested that local network work stop.Mark the item cancelled; do not present cancellation as a server-side deletion.

Retry without creating duplicate files

A timeout is ambiguous: the server may have stored the file even though the response never reached the browser. Retrying a plain create request can therefore create a duplicate. A robust API gives each logical upload an idempotency key, reuses that key for retries of the same upload, and returns the original result when it has already completed. A new user-selected file gets a new key, even if its filename matches.

Use bounded retries for transient failures, such as a connection reset or a temporary server error. Do not automatically retry every response: validation errors, permission failures, and an expired session need a user or application-level action. Preserve the file and error details so the user can correct the problem and retry without selecting the file again.

Cancellation, resume, and large files

For small uploads, a single request with cancellation may be enough. Large files or unreliable connections may need a resumable protocol: the server creates an upload session, the client sends numbered chunks, and the server records which chunks it has accepted. After interruption, the client asks the server for the session status and sends only missing chunks. This requires API support; keeping a File object in React state alone cannot resume an upload after a page reload.

  1. Create a server-side upload session and receive its ID.
  2. Send file chunks with stable session and chunk identifiers.
  3. Retry failed chunks using the same identifiers so repeated requests are safe.
  4. Ask the server which chunks it has, then continue with the missing ones.
  5. Finalize the session and show completion only after the server confirms it.
ts
const controller = new AbortController();

async function uploadFile(file: File, signal: AbortSignal) {
  const form = new FormData();
  form.append("file", file);

  const response = await fetch("/api/uploads", {
    method: "POST",
    body: form,
    signal,
    headers: { "Idempotency-Key": crypto.randomUUID() }
  });

  if (!response.ok) throw new Error(`Upload failed: ${response.status}`);
  return response.json() as Promise<{ id: string }>;
}

// Keep the same idempotency key for retries of this logical upload.

In production code, create the idempotency key once when the user starts a logical upload, store it with that upload item, and reuse it on retries. Do not generate a fresh key inside every attempt, or the server cannot recognize retries as the same operation. Also keep request logic outside the rendering component where practical, so tests can exercise cancellation, stale responses, and error handling without relying on UI timing.

A practical checklist

  • Give each selected file a stable client-side ID and each logical upload a server-recognized idempotency key.
  • Represent uploading, failure, cancellation, and completion as distinct states.
  • Ignore results from attempts that are no longer current.
  • Do not equate 100% transfer progress with successful server-side storage.
  • Make retries bounded and appropriate to the error; resume only when the API supports it.
  • Test the awkward cases: cancel during transfer, timeout after bytes are sent, retry after failure, and responses arriving out of order.

File uploads become a meaningful architecture concern when an application handles large files, background workflows, or multiple concurrent operations. For products where those interactions need to work together reliably, WebMriya develops complex web applications with clear boundaries between interface state and server-side operations.

Frequently asked questions

Does 100% upload progress mean the file is saved?

No. It usually means the browser has sent the request body. Show completion only after the server confirms that it accepted and stored the file.

How do I prevent duplicate files when retrying?

Use a server-supported idempotency key for each logical upload, and reuse it for retries. A timeout does not tell the client whether the server completed the original request.

Can a React app resume an upload after a page reload?

Not on its own. Resuming requires a server-side upload session and a protocol for tracking accepted chunks; the browser must also be able to access the file again.