Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

Storage

File upload with progress tracking.

import { useBunBase, useUpload } from "@bunbase/react-sdk";
function AvatarUpload() {
const { client } = useBunBase();
const { upload, loading, progress, error, data, reset } = useUpload();
async function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0];
if (file) await upload(file, { bucket: "avatars", isPublic: true });
}
return (
<div>
<input type="file" accept="image/*" onChange={handleChange} />
{loading && (
<div>
<progress value={progress} max={1} />
<span>{Math.round(progress * 100)}%</span>
</div>
)}
{data && (
<img
src={client.storage.downloadUrl(data.id, data.filename ?? undefined)}
alt="Uploaded"
/>
)}
{error && (
<p>
{error.message}
<button onClick={reset}>Try again</button>
</p>
)}
</div>
);
}

Each useUpload() call is independent — use one per upload slot:

function MultiUpload() {
const cover = useUpload();
const avatar = useUpload();
return (
<>
<input type="file" onChange={(e) => {
const f = e.target.files?.[0];
if (f) cover.upload(f, { bucket: "covers" });
}} />
<input type="file" onChange={(e) => {
const f = e.target.files?.[0];
if (f) avatar.upload(f, { bucket: "avatars" });
}} />
</>
);
}

FieldTypeDescription
upload(file, options?) => Promise<FileRecord>Start the upload
loadingbooleanTrue while upload is in progress
progressnumberUpload progress from 0 to 1. Resets to 0 on each new upload
dataFileRecord | undefinedThe uploaded file record on success
errorError | nullUpload error
reset() => voidClear data, error, and progress
OptionDescription
bucketTarget bucket name. Defaults to the server’s default bucket
collectionAssociate the uploaded file with a collection
recordIdAssociate the uploaded file with a specific record
isPublicMake the uploaded file directly accessible without auth