Skip to content

Instantly share code, notes, and snippets.

@monis01
Last active November 15, 2022 02:16
Show Gist options
  • Save monis01/2b187cf1844b4d424382057faba2ca2b to your computer and use it in GitHub Desktop.
Save monis01/2b187cf1844b4d424382057faba2ca2b to your computer and use it in GitHub Desktop.
Detect file extension with javascript FileReader
//@ 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