CxJS

UploadButton

import { UploadButton } from 'cx/widgets'; Copied

The UploadButton widget provides file selection and upload functionality. It displays a styled button that opens a file picker, then uploads selected files to a specified URL using XMLHttpRequest. The button shows upload progress and supports validation before upload.

<div class="flex flex-col gap-4" controller={PageController}>
  <div class="flex flex-wrap gap-2">
    <UploadButton
      icon="upload"
      url="https://api.cxjs.io/uploads"
      accept="image/*"
      onUploadStarting={(xhr, instance, file) =>
        instance
          .getControllerByType(PageController)
          .onUploadStarting(xhr, instance, file)
      }
      onUploadProgress={(event, instance) =>
        instance.getControllerByType(PageController).onUploadProgress(event)
      }
      onUploadComplete={(xhr, instance) =>
        instance.getControllerByType(PageController).onUploadComplete(xhr)
      }
      onUploadError={(error, instance) =>
        instance.getControllerByType(PageController).onUploadError(error)
      }
    >
      Upload Image
    </UploadButton>
    <UploadButton icon="upload" url="https://api.cxjs.io/uploads" disabled>
      Disabled
    </UploadButton>
  </div>
  <div class="text-sm text-gray-600" text={m.status} />
</div>
Upload Image
Disabled

Dynamic URLs

For cloud uploads that require pre-signed URLs, use onResolveUrl:

onResolveUrl={async (file, instance) => {
  const response = await fetch("/api/get-upload-url", {
    method: "POST",
    body: JSON.stringify({ filename: file.name }),
  });
  const { url } = await response.json();
  return url;
}}

Configuration

Core Properties

PropertyTypeDefaultDescription
urlstringURL to upload files to
textstringText displayed on the button
iconstringIcon name or configuration
acceptstringFile types accepted (e.g., "image/*", ".pdf")
multiplebooleanfalseAllow multiple file selection
methodstring"POST"HTTP method used for upload
disabledbooleanfalseDisables the button
abortOnDestroybooleanfalseAbort pending uploads when component is destroyed
uploadInProgressTextstring"Upload is in progress."Validation message shown during upload

Callbacks

PropertyTypeDescription
onResolveUrlfunction(file, instance) => string|Promise<string> - Resolve dynamic upload URL for each file
onUploadStartingfunction(xhr, instance, file, formData) => boolean|Promise<boolean> - Return false to cancel
onUploadCompletefunction(xhr, instance, file, formData) => void - Called when upload completes
onUploadProgressfunction(event, instance, file, formData) => void - Called with progress updates
onUploadErrorfunction(error, instance, file, formData) => void - Called when an error occurs