skip to Main Content

Right now, during uploading file(s) all type of files are previewing. Although document type file is not previewing, it’s previewing like when an image source not found. However, I only want to preview the file(s) which are image & video. I don’t want to preview the document type files in my file preview section of HTML.
Have a look under the Send button area

2

Answers


  1. You can use a regular expression to check the file extension type, example below:

    // name of file
    var filename = "file.txt";
    
    // use a regex to check file type
    // | = "or" operator
    // check for multiple file types
    if(filename.match(/.txt|.doc|.pdf|.psd/)){
        alert('Only image & video files are available for preview')
    } else {
     // execute my logic
    
    }
    
    Login or Signup to reply.
  2. if you want to check if filename match one of specific extensions, you can use the following function

    const isExtensionMatch = (filename, matchedExtensions) => {
      const value = filename.split('.');
    
      let extension;
      if (value.length > 1) {
        extension = value[value.length - 1];
      }
    
      return new RegExp('^(' + matchedExtensions.join('|') + ')$').test(extension);
    };
    
    console.log(isExtensionMatch('filename.png', ['png', 'jpg'])); // true
    console.log(isExtensionMatch('filename.sometext.png', ['png', 'jpg'])); // true
    console.log(isExtensionMatch('filename.sometext.jpg', ['png', 'jpe?g'])); // true
    console.log(isExtensionMatch('filename.sometext.jpeg', ['png', 'jpe?g'])); // true
    console.log(isExtensionMatch('filename.sometext.jpeg', ['png', 'mp4'])); // false
    console.log(isExtensionMatch('filename.sometext.jpeg.sometext', ['png', 'jpe?g'])); // false
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search