nsarrazin HF Staff commited on
Commit
cb022e9
·
unverified ·
1 Parent(s): 5ddf84d

feat(upload): Enable multi-file in dropzone (#1347)

Browse files
src/lib/components/chat/FileDropzone.svelte CHANGED
@@ -15,34 +15,37 @@
15
  if (files.length > 0) {
16
  files = [];
17
  }
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
21
  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
- }
 
 
 
 
 
 
 
 
 
 
36
 
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;
42
  }
43
- files = [file];
44
- onDrag = false;
45
  }
 
46
  }
47
  }
48
  }
 
15
  if (files.length > 0) {
16
  files = [];
17
  }
 
 
 
18
  if (event.dataTransfer.items[0].kind === "file") {
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
+ }
43
 
44
+ // add the file to the files array
45
+ files = [...files, file];
 
 
 
46
  }
 
 
47
  }
48
+ onDrag = false;
49
  }
50
  }
51
  }