Better Upload

Client Hooks

Better Upload provides React hooks that allow you to easily upload files using pre-signed URLs. Multipart uploads are managed automatically, in case you enable them in the server.

Usage

Single file

To upload single files, use the useUploadFile hook.

import { useUploadFile } from 'better-upload/client';
 
export function MyComponent() {
  const {
    upload,
    uploadedFile,
    progress,
    isPending,
    isSuccess,
    isError,
    error,
    reset,
  } = useUploadFile({
    route: 'demo',
  });
 
  return (
    <input
      type="file"
      onChange={(e) => {
        if (e.target.files?.[0]) {
          upload(e.target.files[0]);
        }
      }}
    />
  );
}

API Endpoint

If your upload route handler is not located at /api/upload, you need to specify the correct path in the api option.

Multiple files

To upload multiple files at once, use the useUploadFiles hook.

import { useUploadFiles } from 'better-upload/client';
 
export function MyComponent() {
  const {
    upload,
    uploadedFiles,
    progresses,
    isPending,
    isSuccess,
    isError,
    error,
    reset,
  } = useUploadFiles({
    route: 'demo',
  });
 
  return (
    <input
      type="file"
      multiple
      onChange={(e) => {
        if (e.target.files) {
          upload(e.target.files);
        }
      }}
    />
  );
}

Sequential upload

With multiple files, you can also upload sequentially, rather than in parallel. This can be useful to reduce the load on the client and S3 server, however, it will take longer to upload all files.

useUploadFiles({
  route: 'demo',
  sequential: true,
});

Options

Both hooks have the following options:

  • api: The API endpoint to use for uploading files. Default is /api/upload.
  • route: The route to use for uploading files. Needs to match the upload route in the server.
  • multipartBatchSize: The number of parts to upload at the same time when uploading a multipart file. By default, all parts are uploaded in parallel.

Events

On before upload

Callback that is called before requesting the pre-signed URLs. Use this to modify the files before uploading them, like resizing or compressing. You can also throw an error to reject the file upload.

useUploadFile({
  route: 'demo',
  onBeforeUpload: ({ file }) => {
    // rename the file
    return new File([file], 'renamed-' + file.name, { type: file.type })
  },
});

On upload begin

Event that is called when the file starts being uploaded to the S3 bucket. Happens after your server responds with a pre-signed URL.

useUploadFile({
  route: 'demo',
  onUploadBegin: ({ file, metadata }) => {
    console.log('Upload begin');
  },
});

On upload progress

Event that is called when the file upload progress changes.

useUploadFile({
  route: 'demo',
  onUploadProgress: ({ file, progress }) => {
    console.log(`Upload progress: ${progress * 100}%`);
  },
});

On upload complete

Event that is called after the file has been successfully uploaded.

useUploadFile({
  route: 'demo',
  onUploadComplete: ({ file, metadata }) => {
    console.log('File uploaded');
  },
});

On upload error

Event that is called if the upload fails.

useUploadFile({
  route: 'demo',
  onUploadError: (error) => {
    console.log(error.message);
  },
});

On upload settled

Event that is called when the file upload has settled, regardless of success or failure.

useUploadFile({
  route: 'demo',
  onUploadSettled: () => {
    console.log('Upload settled');
  },
});

On this page