Skip to content

Commit c6c64e7

Browse files
author
Ruslan Farkhutdinov
committed
Chat: Implement uploading/downloading in FileAttachments demo
1 parent 6868d74 commit c6c64e7

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

apps/demos/Demos/Chat/FileAttachments/jQuery/data.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ const supportAgent = {
1616
avatarUrl: '../../../../images/petersmith.png',
1717
};
1818

19+
const createEmptyAttachment = (name, type) => {
20+
const blob = new Blob([''], { type });
21+
const url = URL.createObjectURL(blob);
22+
23+
return {
24+
name,
25+
url,
26+
size: 0,
27+
};
28+
};
29+
30+
const screenshot1 = createEmptyAttachment('Screenshot1.jpg', 'image/jpeg');
31+
const screenshot2 = createEmptyAttachment('Screenshot2.jpg', 'image/jpeg');
32+
const screenshot3 = createEmptyAttachment('Screenshot3.jpg', 'image/jpeg');
33+
const instructions = createEmptyAttachment('Instructions.pdf', 'application/pdf');
34+
1935
const messages = [
2036
{
2137
id: new DevExpress.data.Guid(),
@@ -28,32 +44,14 @@ const messages = [
2844
timestamp: getTimestamp(date, -7),
2945
author: currentUser,
3046
text: "Hi, I'm having trouble accessing my account.\nIt says my password is incorrect. I’ve attached some screenshots for you to check.",
31-
attachments: [
32-
{
33-
name: 'Screenshot1.jpg',
34-
size: 0,
35-
},
36-
{
37-
name: 'Screenshot2.jpg',
38-
size: 0,
39-
},
40-
{
41-
name: 'Screenshot3.jpg',
42-
size: 0,
43-
},
44-
],
47+
attachments: [screenshot1, screenshot2, screenshot3],
4548
},
4649
{
4750
id: new DevExpress.data.Guid(),
4851
timestamp: getTimestamp(date, -7),
4952
author: supportAgent,
5053
text: 'Thanks for the screenshots! I can help you with that. Please refer to the attached file for instructions to restore access.',
51-
attachments: [
52-
{
53-
name: 'Instructions.pdf',
54-
size: 0,
55-
},
56-
],
54+
attachments: [instructions],
5755
},
5856
];
5957

apps/demos/Demos/Chat/FileAttachments/jQuery/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,36 @@ $(() => {
1313
paginate: false,
1414
});
1515

16+
const uploadedFilesMap = new Map();
17+
1618
$('#chat').dxChat({
1719
height: 600,
1820
dataSource,
1921
reloadOnChange: false,
2022
user: currentUser,
2123
fileUploaderOptions: {
22-
uploadUrl: 'https://js.devexpress.com/Demos/NetCore/FileUploader/Upload',
24+
uploadFile: () => {},
25+
onValueChanged(e) {
26+
e.value.forEach((file) => {
27+
const url = URL.createObjectURL(file);
28+
uploadedFilesMap.set(file.name, url);
29+
});
30+
},
2331
},
2432
onMessageEntered(e) {
2533
const { message } = e;
2634

35+
const attachmentsWithUrls = message.attachments?.map((attachment) => {
36+
const url = uploadedFilesMap.get(attachment.name);
37+
return { ...attachment, url };
38+
});
39+
2740
dataSource.store().push([{
2841
type: 'insert',
2942
data: {
3043
id: new DevExpress.data.Guid(),
3144
...message,
45+
attachments: attachmentsWithUrls,
3246
},
3347
}]);
3448
},
@@ -51,7 +65,17 @@ $(() => {
5165
}]);
5266
},
5367
onAttachmentDownloadClick(e) {
54-
console.log(e);
68+
const { attachment } = e;
69+
if (!attachment?.url) return;
70+
71+
const link = document.createElement('a');
72+
link.href = attachment.url;
73+
link.download = attachment.name;
74+
document.body.appendChild(link);
75+
76+
link.click();
77+
78+
document.body.removeChild(link);
5579
},
5680
}).dxChat('instance');
5781
});

0 commit comments

Comments
 (0)