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.
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.
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.
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.
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.
On upload progress
Event that is called when the file upload progress changes.
On upload complete
Event that is called after the file has been successfully uploaded. If some files failed to upload, they will be in the failedFiles
array.
On upload error
Event that is called if the upload fails. This can be called more than once for multiple files.
On upload settled
Event that is called when the file upload has settled, regardless of success or failure.
Error handling
Failed multiple files
When uploading multiple files, although rare, some files may fail to upload. In this case, the failedFiles
array will contain the files that failed to upload.
If this happens, isSuccess
and isError
will be true
, and the upload
complete event will be called.