filereader check file type

Solutions on MaxInterview for filereader check file type by the best coders in the world

showing results for - "filereader check file type"
Anna
22 Mar 2020
1var fileTypes = ['jpg', 'jpeg', 'png', 'what', 'ever', 'you', 'want'];  //acceptable file types
2
3function readURL(input) {
4    if (input.files && input.files[0]) {
5        var extension = input.files[0].name.split('.').pop().toLowerCase(),  //file extension from input file
6            isSuccess = fileTypes.indexOf(extension) > -1;  //is extension in acceptable types
7
8        if (isSuccess) { //yes
9            var reader = new FileReader();
10            reader.onload = function (e) {
11                alert('image has read completely!');
12            }
13
14            reader.readAsDataURL(input.files[0]);
15        }
16        else { //no
17            //warning
18        }
19    }
20}
21