naystack - v1.9.9
    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 | NativeFile | File,
          type: string,
          config?: { async?: boolean; data?: object },
      ) => Promise<FileUploadResponseType>

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

      • file — File or Blob (web) or a { uri, name, type } NativeFile descriptor (React Native) to upload.
      • type — String category (e.g. "avatar", "DealDocument"); sent as form field type.
      • config — Optional object with:
        • data — JSON-serializable metadata object; sent as form field data.
        • async — If true, the upload is processed asynchronously on the server. 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", {
      data: { 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])} />;
      }