DevExtreme v25.2 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

JavaScript/jQuery Chat - File Attachments

The DevExtreme JavaScript Chat allows users to attach files. When this feature is activated (fileUploaderOptions.uploadFile is specified), an “Attach” button appears in the message input field, allowing users to add files to their messages.

When users attach files, each file is displayed in the input area with a file-type icon, basic details (name and size), upload status, and an option to remove files before sending.

Backend API
$(() => { const customStore = new DevExpress.data.CustomStore({ key: 'id', load: async () => messages, insert: async (message) => { messages.push(message); return message; }, }); const dataSource = new DevExpress.data.DataSource({ store: customStore, paginate: false, }); const uploadedFilesMap = new Map(); function getFileUrl(filename) { return uploadedFilesMap.get(filename); } $('#chat').dxChat({ height: 710, dataSource, reloadOnChange: false, user: currentUser, fileUploaderOptions: { uploadFile: () => {}, onUploaded({ file }) { const url = URL.createObjectURL(file); uploadedFilesMap.set(file.name, url); }, }, onMessageEntered(e) { const { message } = e; const attachmentsWithUrls = message.attachments?.map((attachment) => ({ ...attachment, url: getFileUrl(attachment.name), })); dataSource.store().push([{ type: 'insert', data: { id: new DevExpress.data.Guid(), ...message, attachments: attachmentsWithUrls, }, }]); }, onAttachmentDownloadClick(e) { const { attachment } = e; if (!attachment?.url) { return; } const link = document.createElement('a'); link.setAttribute('href', attachment.url); link.setAttribute('download', attachment.name); document.body.appendChild(link); link.click(); document.body.removeChild(link); }, }).dxChat('instance'); });
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/25.2.4/css/dx.light.css" /> <script src="js/dx.all.js?v=25.2.4"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="data.js"></script> <script src="index.js"></script> </head> <body class="dx-viewport"> <div class="demo-container"> <div class="chat-container"> <div id="chat"></div> </div> </div> </body> </html>
.demo-container { min-width: 720px; } .chat-container { display: flex; flex-grow: 1; align-items: center; justify-content: center; } .dx-chat { max-width: 480px; } .caption { font-size: var(--dx-font-size-sm); font-weight: 500; } .dx-avatar { border: 1px solid var(--dx-color-border); }
const getTimestamp = function (date, offsetMinutes = 0) { return date.getTime() + offsetMinutes * 60000; }; const date = new Date(); date.setHours(0, 0, 0, 0); const currentUser = { id: 'c94c0e76-fb49-4b9b-8f07-9f93ed93b4f3', name: 'John Doe', }; const supportAgent = { id: 'd16d1a4c-5c67-4e20-b70e-2991c22747c3', name: 'Support Agent', avatarUrl: '../../../../images/petersmith.png', }; const messages = [ { id: new DevExpress.data.Guid(), timestamp: getTimestamp(date, -7), author: currentUser, text: "Hi! I'm having trouble accessing my account.\nThe website says my password is incorrect. I'm sending a few screenshots so you can see where I get the error.", attachments: [ { name: 'Pic1.png', url: '../../../../images/Chat/FileAttachments/Pic1.png', size: 1024 * 10, }, { name: 'Pic2.png', url: '../../../../images/Chat/FileAttachments/Pic2.png', size: 1024 * 10, }, { name: 'Pic3.png', url: '../../../../images/Chat/FileAttachments/Pic3.png', size: 1024 * 10, }, ], }, { id: new DevExpress.data.Guid(), timestamp: getTimestamp(date, -7), author: supportAgent, text: 'Hello! Thanks for including screenshots. To restore access, please follow instructions in the attached file.\nLet me know if you need anything else.', attachments: [ { name: 'Instructions.pdf', url: '../../../../images/Chat/FileAttachments/Instructions.pdf', size: 1024 * 10, }, ], }, ];

You can customize the file upload process with the following fileUploaderOptions properties:

  • maxFileSize
    Specifies maximum allowed file size.
  • minFileSize
    Specifies minimum allowed file size.
  • multiple
    When set to false, limits uploads to a single file.
  • allowedFileExtensions
    Restricts accepted file types.

For the complete list of configuration options, refer to the following API section: fileUploaderOptions.

Attachment type includes name and size fields. To add custom fields (such as url in this demo), handle the onMessageEntered event and update the message object’s attachments array as needed. You can use this handler to save files to your server.

After a user sends a message, attachments appear in the corresponding message bubble. To allow users to download attachments, implement the onAttachmentDownloadClick event handler. You can define custom download logic within the handler.