Skip to content

Commit 1c561d2

Browse files
author
Jasmin
committed
On folder/files drop validate file extension
1 parent e17aa41 commit 1c561d2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/ngx-file-drop/ngx-file-drop.component.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class NgxFileDropComponent implements OnDestroy {
235235
private traverseFileTree(item: FileSystemEntry, path: string): void {
236236
if (item.isFile) {
237237
const toUpload: NgxFileDropEntry = new NgxFileDropEntry(path, item);
238-
this.files.push(toUpload);
238+
this.addToQueue(toUpload);
239239

240240
} else {
241241
path = path + '/';
@@ -332,9 +332,28 @@ export class NgxFileDropComponent implements OnDestroy {
332332
}
333333

334334
private addToQueue(item: NgxFileDropEntry): void {
335+
const isAcceptedExt = this.isAcceptedExtension(this.accept, item.fileEntry.name);
336+
if (!isAcceptedExt) return;
337+
335338
this.files.push(item);
336339
}
337340

341+
private getFileExtension(fileName: string) {
342+
const extension = fileName.split('.').slice(-1)[0];
343+
return `.${extension}`;
344+
}
345+
346+
private isAcceptedExtension(acceptedExtensions: string, fileName: string) {
347+
if (acceptedExtensions === "*") {
348+
return true;
349+
}
350+
351+
const fileExtension = this.getFileExtension(fileName);
352+
353+
const acceptedExtList = acceptedExtensions.split(',');
354+
return acceptedExtList.includes(fileExtension);
355+
}
356+
338357
private preventAndStop(event: Event): void {
339358
event.stopPropagation();
340359
event.preventDefault();

0 commit comments

Comments
 (0)