Skip to content

Commit ffc0ac4

Browse files
authored
feat(upload): Enable multi-file in dropzone (#1347)
1 parent fc2c114 commit ffc0ac4

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/lib/components/chat/FileDropzone.svelte

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,37 @@
1515
if (files.length > 0) {
1616
files = [];
1717
}
18-
// get only the first file
19-
// optionally: we need to handle multiple files, if we want to support document upload for example
20-
// for multimodal we only support one image at a time but we could support multiple PDFs
2118
if (event.dataTransfer.items[0].kind === "file") {
22-
const file = event.dataTransfer.items[0].getAsFile();
23-
if (file) {
24-
// check if the file matches the mimeTypes
25-
if (
26-
!mimeTypes.some((mimeType: string) => {
27-
const [type, subtype] = mimeType.split("/");
28-
const [fileType, fileSubtype] = file.type.split("/");
29-
return type === fileType && (subtype === "*" || fileSubtype === subtype);
30-
})
31-
) {
32-
setErrorMsg(`File type not supported. Only allowed: ${mimeTypes.join(", ")}`);
33-
files = [];
34-
return;
35-
}
19+
for (let i = 0; i < event.dataTransfer.items.length; i++) {
20+
const file = event.dataTransfer.items[i].getAsFile();
21+
22+
if (file) {
23+
// check if the file matches the mimeTypes
24+
// else abort
25+
if (
26+
!mimeTypes.some((mimeType: string) => {
27+
const [type, subtype] = mimeType.split("/");
28+
const [fileType, fileSubtype] = file.type.split("/");
29+
return type === fileType && (subtype === "*" || fileSubtype === subtype);
30+
})
31+
) {
32+
setErrorMsg(`Some file type not supported. Only allowed: ${mimeTypes.join(", ")}`);
33+
files = [];
34+
return;
35+
}
36+
37+
// if file is bigger than 10MB abort
38+
if (file.size > 10 * 1024 * 1024) {
39+
setErrorMsg("Some file is too big. (10MB max)");
40+
files = [];
41+
return;
42+
}
3643
37-
// if file is bigger than 10MB abort
38-
if (file.size > 10 * 1024 * 1024) {
39-
setErrorMsg("Image is too big. (2MB max)");
40-
files = [];
41-
return;
44+
// add the file to the files array
45+
files = [...files, file];
4246
}
43-
files = [file];
44-
onDrag = false;
4547
}
48+
onDrag = false;
4649
}
4750
}
4851
}

0 commit comments

Comments
 (0)