Last active
November 15, 2022 02:16
-
-
Save monis01/2b187cf1844b4d424382057faba2ca2b to your computer and use it in GitHub Desktop.
Detect file extension with javascript FileReader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@ https://stackoverflow.com/questions/25095863/how-to-detect-file-extension-with-javascript-filereader | |
var fileTypes = ['jpg', 'jpeg', 'png', 'what', 'ever', 'you', 'want']; //acceptable file types | |
function readURL(input) { | |
if (input.files && input.files[0]) { | |
var extension = input.files[0].name.split('.').pop().toLowerCase(), //file extension from input file | |
isSuccess = fileTypes.indexOf(extension) > -1; //is extension in acceptable types | |
if (isSuccess) { //yes | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
alert('image has read completely!'); | |
} | |
reader.readAsDataURL(input.files[0]); | |
} | |
else { //no | |
//warning | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment