A function (file, type, data?) => Promise<FileUploadResponseType | null>.
file — File or Blob to upload.type — String category (e.g. "avatar", "DealDocument"); sent as form field type.data — Optional JSON-serializable object for metadata; sent as form field data.
Resolves to the JSON response { url, onUploadResponse } or null.import { useFileUpload } from "naystack/file/client";
function FileUploader({ dealId }: { dealId: number }) {
const uploadFile = useFileUpload();
const [uploading, setUploading] = useState(false);
const handleUpload = async (file: File) => {
setUploading(true);
try {
const result = await uploadFile(file, "DealDocument", {
dealId,
fileName: file.name,
category: "Contract",
});
if (result?.url) {
console.log("Uploaded:", result.url);
router.refresh();
}
} finally {
setUploading(false);
}
};
return <input type="file" onChange={(e) => e.target.files?.[0] && handleUpload(e.target.files[0])} />;
}
Returns a function that uploads a file to your file upload endpoint with the current auth token. Must be used inside a tree that provides the token (i.e. inside
AuthWrapper).The endpoint is read from
NEXT_PUBLIC_FILE_ENDPOINTenv var. The file is sent as multipart form data along with atypestring and optional JSONdatafor metadata.