
How to check file is an image in javascript using filename
How to check file is an image in javascript using filename
In this tutorial, we will try to find the solution to "How to check file is an image in javascript using filename" through programming.
You can check if the file is an image using the filename in javascript. This function helps you get the file extension and return true or false if the file extension does not exist in the provided file extensions array.-
Check the file extension in javascript using filename and return true if the file is an image
<script> function getFileExtension(fileName){ var fileExtension; fileExtension = fileName.replace(/^.*\./, ''); return fileExtension; } function isIMage(fileName){ var fileExt = getFileExtension(fileName); var imagesExtension = ["png", "jpg", "jpeg"]; if(imagesExtension.indexOf(fileExt) !== -1){ return true; } else{ return false; } } console.log(isIMage('filename.jpg')); </script>
You have to pass the filename to function. This function first checks the file extension and after that it returns true and false on the basis of the provided file extension array.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- Uncaught TypeError: $(...).summernote is not a function
- How to pass array as arguments to function in javascript
- Input type file styling
- Javascript get data-id attribute value
- How to get data from localstorage in javascript
- Javascript get only 10 characters from string
- Remove hostname from URL in javascript
- Concat variable with string in javascript
- How to push string in an array in Javascript
- Uncaught TypeError: Illegal invocation
- How to set href value of anchor tag in javascript
- How to get the length of a number in JavaScript
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- Prism js horizontally overflowing its container
- Check if value exists in array javascript
- Short-circuits conditionals
- Javascript set timeout function with example code
- How to Set data to localstorage in javascript
- How to use JavaScript to search for items in a list
- How to get file extension using javascript/jquery
- How to add key/value pair to existing form data
- ReferenceError: Cannot access 'myFunction' before initialization
- How to make entire Div clickable in javascript
- How to crop multiple images with cropper js
- How to remove duplicate values from array in javascript