1

Is it possible to read the type of file selected in an input field when on browsers (like IE8) that don't support javaScript FileReader?

I want to detect if the selected file is an image or not. I will have server side validation, but would also like the user to know if the file is correct before submitting. I've tried a number of different solutions but can't find any support for IE8...

This javascript doesn't work in IE8:

<input id="uploadImage" type="file"name="myPhoto" onchange="isImage();" />

var upload_image = document.getElementById("uploadImage");
function isImage() {
    if (
        upload_image.files[0].type == 'image/jpg' ||
        upload_image.files[0].type == 'image/jpeg' ||
        upload_image.files[0].type == 'image/png' ||
        upload_image.files[0].type == 'image/gif' ||
        upload_image.files[0].type == 'image/bmp'
        ) {
            return true;
        }
        else return false;
    }

This method doesn't help either:

function isImage(a){
    return a.indexOf(".png") != -1 && a.type == file;
}

The 'accept' parameter also does not work in older IE versions:

<input name="img1" id="img1" type="file" accept="image" />
Patrick Keane
  • 623
  • 3
  • 17
  • 37
  • 1
    You really need to inspect the files on the server anyway, so for incapable clients you can just allow anything and relay any server-generated errors. – Pointy Dec 30 '13 at 15:07
  • For IE or browser that don't support FileReader you could use a fallback to check the file extension. – t.niese Dec 30 '13 at 15:16
  • possible duplicate of [Validation of file extension before uploading file](http://stackoverflow.com/questions/4234589/validation-of-file-extension-before-uploading-file) – Liam Dec 30 '13 at 15:21
  • 1
    You can certainly check the file extension, but there's no reason for somebody crafting an exploit to follow file naming rules. The server really needs to inspect the file contents. – Pointy Dec 30 '13 at 16:29
  • I had a suspicion it couldn't be done, or at least not easily. – Patrick Keane Dec 31 '13 at 12:46

0 Answers0