Compruebe el tamaño del archivo antes de subir
Estoy usando este javascript que obtuve de aquí, y funciona perfectamente para lo que necesito.
var _validFileExtensions = [".jpg", ".jpeg"];
function File_Validator(theForm){
var arrInputs = theForm.getElementsByTagName("input");
for (var i = 0; i < arrInputs.length; i++) {
var oInput = arrInputs[i];
if (oInput.type == "file") {
var sFileName = oInput.value;
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("Invalid image file type!");
return false;
}
}
}
return true;
}
Ahora, me preguntaba si, además, podría verificar el tamaño del archivo y fallar si el archivo es más grande que 500kb -> ¿todos antes de presionar el botón enviar / cargar?
EDITAR
Después de ver lo que sugirió PHPMyCoder, termino resolviendo usando este código javascript:
<script language='JavaScript'>
function checkFileSize(inputFile) {
var max = 3 * 512 * 512; // 786MB
if (inputFile.files && inputFile.files[0].size > max) {
alert("File too large."); // Do your thing to handle the error.
inputFile.value = null; // Clear the field.
}
}
</script>
Esto verifica el tamaño del archivo y alerta al usuario antes de enviar el formulario.