Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ngx-file-drop allow only image or pdf

//trick for max file size and file type check in ngx-file-drop
public dropped(files: NgxFileDropEntry[]) {
    if (files.length > 6) {
      this.toastr.error("Cannot add more than 6 Files at a time.")
    } else {
      // this.files = files;
      for (const droppedFile of files) {

        // Is it a file?
        if (droppedFile.fileEntry.isFile && this.isFileAllowed(droppedFile.fileEntry.name)) {

          const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
          fileEntry.file((file: File) => {

            if (this.isFileSizeAllowed(file.size)) {
              //files array is used to display
              //the files to the user which will be uploaded
              this.files.push(droppedFile);
              if (this.files.length < 6) {

                // Here you can access the real file
                console.log(droppedFile.relativePath, file);
                this.formData.append(`img${this.index}`, file, droppedFile.relativePath);
                this.index++;

                /**
                // You could upload it like this:
                const formData = new FormData()
                formData.append('logo', file, relativePath)
       
                // Headers
                const headers = new HttpHeaders({
                  'security-token': 'mytoken'
                })
       
                this.http.post('https://mybackend.com/api/upload/sanitize-and-save-logo', formData, { headers: headers, responseType: 'blob' })
                .subscribe(data => {
                  // Sanitized logo returned from backend
                })
                **/
              } else {
                this.toastr.error("Maximum 6 files are allowed.");
              }
            } else {
              this.toastr.error("Max size of a file allowed is 1mb, files with size more than 1mb are discarded.");
            }
          });


        } else {
          // It was a directory (empty directories are added, otherwise only files)
          this.toastr.error("Only files in '.pdf', '.jpg', '.jpeg', '.png' format are accepted and directories are not allowed.");
          // const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
          // console.log(droppedFile.relativePath, fileEntry);
        }
      }
    }
  }

  isFileAllowed(fileName: string) {
    let isFileAllowed = false;
    const allowedFiles = ['.pdf', '.jpg', '.jpeg', '.png'];
    const regex = /(?:.([^.]+))?$/;
    const extension = regex.exec(fileName);
    if (undefined !== extension && null !== extension) {
      for (const ext of allowedFiles) {
        if (ext === extension[0]) {
          isFileAllowed = true;
        }
      }
    }
    return isFileAllowed;
  }

  isFileSizeAllowed(size) {
    let isFileSizeAllowed = false;
    if (size < 1000000) {
      isFileSizeAllowed = true;
    }
    return isFileSizeAllowed;

  }
Comment

PREVIOUS NEXT
Code Example
Typescript :: angular typescript refresh page 
Typescript :: Publication only contains dependencies and/or constraints without a version. You need to add minimal version information, publish resolved versions 
Typescript :: try catch powershell error message 
Typescript :: how to add jwt token in angular http request 
Typescript :: debounce typescript 
Typescript :: npm clean 
Typescript :: conditional (click) action angular 
Typescript :: array with objects read element with the lowest value 
Typescript :: html collection of elements to array 
Typescript :: create if not exists rails 
Typescript :: howt o make sure its a valid sudoku in python 
Typescript :: distance between two points latitude longitude c# 
Typescript :: typescript check type of variable 
Typescript :: ts react props type 
Typescript :: number of elements in list in python 
Typescript :: split list into sublists with linq 
Typescript :: c# copy the elements of a list to another list 
Typescript :: typescript compile on save 
Typescript :: typescript append row in html table 
Typescript :: accessing list elements in dictionary python 
Typescript :: react-excel-renderer nextjs error 
Typescript :: await constructor typescript 
Typescript :: get type of element of array typescript 
Typescript :: loop type in typescript 
Typescript :: typescript union types 
Typescript :: get typescript props of component 
Typescript :: craeting a method that can take any number of arguments in python 
Typescript :: typescript playground 
Typescript :: typescript variable 
Typescript :: typescript var global: typeof globalThis 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =