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
If your upload route handler is not located at /api/upload
, you need to
specify the correct path in the api
option.
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 simultaneously.signal
: TheAbortSignal
to use for cancelling the upload.headers
: The headers to send with the pre-signed URL request to your server.
For multiple files, you can also specify:
uploadBatchSize
: The number of files to upload in parallel. By default, all files are uploaded simultaneously.
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.
On upload begin
Event that is called before the files start being uploaded to S3. This happens after the server responds with the pre-signed URL.
On upload progress
Event that is called when a file upload progress changes.
On upload complete
Event that is called after files are successfully uploaded.
On upload fail
Event that is called after the entire upload if a file fails to upload.
On error
Event that is called if:
- For multiple files: A critical error occurs before the upload to S3, and no files were able to be uploaded. For example, if your server is unreachable.
- For single file: The upload fails.
Also called if some input is invalid. For example, if no files were selected.
On upload settle
Event that is called after the upload settles (either successfully completed or an error occurs).