3

I'm using "ngx-file-drop" on Angular 6.

<file-drop headertext="Drop files here" (onFileDrop)="dropped($event)" 
             (onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)" multiple>
                 <span>optional content (don't set headertext then)</span>
             </file-drop>

And component file is

public dropped(event: UploadEvent) {
    this.files = event.files;
    for (const droppedFile of event.files) {

      // Is it a file?
      if (droppedFile.fileEntry.isFile) {
        const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
        fileEntry.file((file: File) => {

          // Here you can access the real file
          // console.log("dropfile_file"+ droppedFile.relativePath, file);

          this.drop_files.push(file);
          console.log(this.drop_files);



        });
      } else {
        // It was a directory (empty directories are added, otherwise only files)
        const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
        console.log("file_entry"+ droppedFile.relativePath, fileEntry);
      }
    }
  }

  public fileOver(event){
    console.log("file_over"+event);
  }

  public fileLeave(event){
    console.log("file_leave"+event);
  }

I've no idea how to validate file to using ngx-file-drop.

Is there any approch in ngx-file-drop to validate file? please help.

Thanks,

1
  • I'm also using this library trying to do a similar thing. I see in the docs there is an input parameter "accept" which gets passed all the way down to the underlying <input> tag, but I dont see this doing anything with the drag and drop functionality. I have the parameter set accept="png" like in the docs and I can still drag and drop any file. Is this expected functionality with this library? I was wondering if there was an out of the box solution and a way to avoid the file extension parsing on drop answers as stated below. Commented Mar 20, 2019 at 14:12

2 Answers 2

3

I made it work like this :

public dropped(event: UploadEvent) {
    if (event.files.length > 1) {
        alert("impossible de rajouter plus d'un document à la fois");
    } else {
        this.verifierEnvoyerDocument(event.files[0]);
    }
}

verifierEnvoyerDocument(droppedFile: UploadFile) {
    this.file = droppedFile;
    // Is it a file and is it allowed?
    if (droppedFile.fileEntry.isFile && this.isFileAllowed(droppedFile.fileEntry.name)) {
        const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
        fileEntry.file((file: File) => {
            console.log('isFile :', file.name);
            console.log(droppedFile.relativePath, file);
            this.envoyerDocument(droppedFile.relativePath, file);
        });
    } else {
        alert("Seul les fichiers au format '.doc', '.docx', '.ppt', '.pptx', '.pdf' sont accepté.");
    }
}

isFileAllowed(fileName: string) {
    let isFileAllowed = false;
    const allowedFiles = ['.doc', '.docx', '.ppt', '.pptx', '.pdf'];
    const regex = /(?:\.([^.]+))?$/;
    const extension = regex.exec(fileName);
    if (isDevMode()) {
        console.log('extension du fichier : ', extension);
    }
    if (undefined !== extension && null !== extension) {
        for (const ext of allowedFiles) {
            if (ext === extension[0]) {
                isFileAllowed = true;
            }
        }
    }
    return isFileAllowed;
}

Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

0
import { UploadEvent, UploadFile, FileSystemFileEntry, FileSystemDirectoryEntry } from 'ngx-file-drop';




public dropped(event: UploadEvent) {
    let droppedFile = event.files[0];
      if (droppedFile.fileEntry.isFile) {
        const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
        fileEntry.file((file: File) => {
          this.file = file;
        });
    }
  }

in multiple loop event.files

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.