Back to Snippets

AI Chat Input with File Attachments JS

A message box for an AI chat that takes files as well as text. Attach files with the plus button, paste an image right into the box, or drag files onto it. Images show as thumbnails and other files as chips with their type and size, each with its own remove button. Enter sends and Shift+Enter adds a new line, and while the reply is on its way the send button turns into a stop button.

Each file is checked against the limits (four files, 10 MB each, an allowed extension and not empty), and anything that fails gets a plain message under the box. Thumbnails use URL.createObjectURL, and the URL is revoked when you remove the file. The keydown handler checks isComposing and keyCode 229, so pressing Enter to confirm a word in a Japanese or Chinese input method does not send the message, even in Safari 26 and earlier, which report isComposing as false on that key.

The two sample files are File objects made in the page, one of them drawn on a canvas, so the demo shows chips without an upload. Nothing leaves the browser, and the reply only lists what it received. To upload for real, send the files to your own endpoint with FormData. The box sits in the middle until the first message, then moves to the bottom while the messages scroll above it.

0
#javascript
#css
#ai
#chat input
#file upload
#drag and drop
#attachments
#textarea