naystack - v1.5.10
    Preparing search index...

    Function useFileUpload

    • 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_ENDPOINT env var. The file is sent as multipart form data along with a type string and optional JSON data for metadata.

      Returns (
          file: Blob | File,
          type: string,
          data?: object,
      ) => Promise<FileUploadResponseType>

      A function (file, type, data?) => Promise<FileUploadResponseType | null>.

      • fileFile 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])} />;
      }