The "accept" attribute of the file input should limit what types of files are uploaded. For example, this should filter it out so one can only choose jpegs and gifs:
HTML Code:
<input type="file" accept="image/jpeg, image/gif" name="file1" />
I know that this doesn't work in IE though (and I don't think other browsers support it either). You can do a validation routine in Javascript to reinforce it though:
Code:
<script type="text/javascript">
function checkFileType(f) {
var ext, valid;
for (var i = 0; i < f.elements.length; i++) {
if (f.elements[i].type == "file") {
ext = f.elements[i].value.substring(f.elements[i].value.lastIndexOf("."), f.elements[i].value.length);
if (ext == "jpg" || ext == "jpeg" || ext == "jpe" || ext == "gif") {
valid = true;
} else {
valid = false;
}
if (!valid) alert('Please choose the right type of file!'); return false;
}
}
}
</script>
HTML Code:
<form id="theForm" onsubmit="return checkFileType(this);">
I know this routine can also be optimized a lot more.
Bookmarks