Skip to main content

REFERENCE

SDK Demos

How to use @asgard-js/core and @asgard-js/react, with demos you can try on the page.

File Upload & Drag-and-Drop

<Chatbot> has built-in image and document upload, and users can also drag and drop files straight onto the chat window. The demo below connects to a real bot provider, so you can actually pick files, preview them, and drag-drop.

info

Uploads require a real botProviderEndpoint. In preview mode (botProviderEndpoint: "skip") the upload buttons still render, but the input is disabled and files are not uploaded.

  1. The two upload toggles​

    PropShown in the footerAccepted files
    enableUploadImage upload buttonJPEG / JPG / PNG / GIF / WebP
    enableDocumentUploadDocument upload buttonPDF, Office, CSV, PPT, MP3 / MP4, HTML / XML / TXT / JSON

    When 3 or more buttons are enabled (including enableExport), the footer collapses them into a single "+" menu.

  2. File limits​

    TypeAllowed formatsPer-file limitCount limit
    ImageJPEG / JPG / PNG / GIF / WebP20MB10 files
    DocumentPDF, Office, CSV, PPT, MP3 / MP4, HTML / XML / TXT / JSON20MB10 files

    Files exceeding the limits are rejected with an error indicator.

  3. Restricting which MIME types can be uploaded​

    By default all formats in the tables above are accepted. To allow only a subset, pass allowedImageMimeTypes (with enableUpload) and/or allowedDocumentMimeTypes (with enableDocumentUpload). The demo below runs in preview mode — pick a preset, then open the attachment menu in the chatbot and the file picker will only offer the matching formats:

    <Chatbot
    // ...
    enableUpload
    enableDocumentUpload
    allowedImageMimeTypes={["image/png", "image/jpeg"]} // images: PNG / JPEG only
    allowedDocumentMimeTypes={["application/pdf"]} // documents: PDF only
    />

    How it behaves:

    • It overrides, not merges: once provided, it fully replaces the default list and applies to both the file picker accept and upload validation.
    • You may include types outside the default list, e.g. ["image/svg+xml"] — the values are used as-is.
    • An empty array is treated as omitted, falling back to the default list.
    • ⚠️ Documents are matched differently: when omitted, validation checks the MIME type or the file extension (some browsers return an empty MIME type); once allowedDocumentMimeTypes is set, it matches the MIME type only — no extension fallback, so pass exact MIME types (e.g. application/pdf).
  4. What gets attached when sending​

    After a user picks files, the SDK uploads them asynchronously right away and shows a preview above the input (image thumbnails / a document list). When the message is sent, this info is included in SendMessageParams:

    FieldDescription
    blobIdsBlob ids of successfully uploaded files (both images and documents)
    filePreviewUrlsLocal preview URLs for images
    documentNamesOriginal filenames for documents

    Use onBeforeSendMessage to read or modify these fields before sending.

  5. Code Example​

    import { Chatbot } from "@asgard-js/react";

    export function Demo() {
    return (
    <Chatbot
    title="File Upload Demo"
    config={{ botProviderEndpoint: BOT_PROVIDER_ENDPOINT }}
    customChannelId="file-upload-demo"
    enableUpload
    enableDocumentUpload
    />
    );
    }
  6. Customizing drag-and-drop: useFileDropContext​

    Drag-and-drop is handled by <Chatbot> out of the box: dragging files over the window shows a drop-zone overlay, and on drop the files are automatically sorted into images / documents and enter the upload flow.

    If you build a custom footer or want to own the drop zone yourself, use useFileDropContext:

    import { useFileDropContext } from "@asgard-js/react";

    function CustomDropZone() {
    const {
    droppedFiles,
    isDraggingOver,
    setDroppedFiles,
    setIsDraggingOver,
    clearDroppedFiles,
    } = useFileDropContext();

    // isDraggingOver: whether the user is dragging files over the window
    // droppedFiles: the File[] after a drop
    // setDroppedFiles / clearDroppedFiles: inject or clear pending files
    return null;
    }
    Field / methodDescription
    droppedFilesThe pending dropped File[]
    isDraggingOverWhether the user is dragging files over the window
    setDroppedFilesSet pending files programmatically
    setIsDraggingOverSet the dragging state (for custom overlays)
    clearDroppedFilesClear pending files
  7. See also​

Ready to get started?

Want to see how Asgard fits your team? Talk to us.

Contact us