I would like to attach an event to all the input type="file" on my website to check if the file size is superior to 5mo, and if so block the upload and display a warning instead. Is it possible?
You can just bind the change event with jQuery:
$('input[type="file"]').change(function(){
var f=this.files[0];
var sizeInMb = f.size/1024;
var sizeLimit= 1024*5; // if you want 5 MB
if (sizeInMb > sizeLimit) {
alert('Sorry the file exceeds the maximum size of 5 MB!');
// reset the input (code for all browser)
this.replaceWith(input.val('').clone(true));
return false;
}
else {
// go on
}
}
Hope this helps.