Skip to main content

Upload progress

An upload form with a progress bar that moves as the bytes go out, plus the speed and the time left. Pick a file up to 1 MB or press Upload a 1 MB test file, and press Cancel to stop halfway. The file goes to httpbin, which echoes what it received.

intermediateuploadsaxiosuseSubmitcancellation
import { AxiosAdapter } from "@hyper-fetch/axios";
import { createClient } from "@hyper-fetch/core";
import type { AxiosError } from "axios";

/**
* The Axios adapter sends requests with XMLHttpRequest in the browser, which reports upload progress.
* Everything else (requests, hooks, cache) stays the same as with the default fetch adapter.
*/
export const client = createClient<{ error: AxiosError }>({ url: "https://httpbin.org" }).setAdapter(AxiosAdapter);

/** httpbin rejects bodies above about 1 MB with 413, so the example keeps files under that. */
export const maxFileSize = 1024 * 1024;

/** httpbin echoes the request. `headers["Content-Length"]` tells how many bytes it received. */
export const uploadFileRequest = client.createRequest<{
response: { headers: Record<string, string>; files: Record<string, string> };
payload: FormData;
}>()({
endpoint: "/post",
method: "POST",
});
Run locallynpx tiged BetterTyped/hyper-fetch/examples/react/upload-progress hyper-fetch-react-upload-progress && cd hyper-fetch-react-upload-progress && npm install && npm run dev
What it shows
  1. The Axios adapter swaps the transport with setAdapter(AxiosAdapter) and reports upload progress.
  2. onSubmitUploadProgress receives progress, loaded, total, timeLeft and startTimestamp for every chunk.
  3. abort() from useSubmit cancels the upload and fires onSubmitAbort.
  4. No other change: requests, hooks and callbacks are the same as with the default adapter.

How it works​

  1. api.ts creates the client with the Axios adapter. The error type is set to AxiosError, so response.error is typed:

    createClient<{ error: AxiosError }>({ url: "https://httpbin.org" }).setAdapter(AxiosAdapter);
  2. uploadFileRequest takes a FormData payload. The browser sets the multipart Content-Type with its boundary.

  3. App.tsx registers onSubmitUploadProgress. In the browser, axios sends with XMLHttpRequest, whose upload.onprogress event fires as chunks leave. The adapter passes each event to HyperFetch, which adds progress in percent and timeLeft in milliseconds. The speed is loaded divided by the time since startTimestamp.

  4. At 100% the body is sent, but the server has not answered yet. The card says so until onSubmitSuccess arrives with the echo, where headers["Content-Length"] shows how many bytes httpbin received.

  5. Cancel calls abort(). Axios stops the upload, the request resolves as aborted, onSubmitAbort fires, and the response is not stored.

  6. httpbin answers 413 for bodies above about 1 MB, so files are checked before they are sent. With your own server, drop that check.

Why the default adapter cannot do this​

The default HTTP adapter uses fetch. fetch has no upload progress events: the browser sends the body without telling the page how much is gone. So with fetch HyperFetch can only report the start and the end of the upload. Download progress works with both adapters, because a fetch response body can be read chunk by chunk. If you only need to show that an upload is running, the file upload example does it with the default adapter.

When to use it
  • Uploads of large files where users need to see that something is happening and how long it takes.
  • Set the Axios adapter on a separate client for uploads if the rest of the app should keep using fetch.

Summary

setAdapter(AxiosAdapter) gives HyperFetch real upload events, and onSubmitUploadProgress turns them into a percentage, speed and time left. fetch cannot report upload progress, so use Axios when the bar has to move.