Skip to main content

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.

Upload Toggles

Drag an image or document straight onto the chatbot on the right to see the drop-zone overlay. Thumbnails / filenames preview above the input before you send.

Current Configuration

{
  "enableUpload": true,
  "enableDocumentUpload": true
}
Loading chatbot...

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.

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.

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.

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

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

See also