Do we have any way using which we can validate File Size before upload in Javascript other then using File API as that is not supported in IE8 and 9.
Files are being selected with file type input tag
Dont want to use Activex
Do we have any way using which we can validate File Size before upload in Javascript other then using File API as that is not supported in IE8 and 9.
Files are being selected with file type input tag
Dont want to use Activex
You can use a polyfill to add support for File API in older browsers such as IE8 and IE9.
Go here and scroll down to the "File API" section.
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills
You can do this using Javascript as shown below,
$('input[type="file"]').change(function () {
if (this.files[0] != undefined) {
var name = this.name;
var filesize = this.files[0].size;
var field = $('#' + this.name).parents('.input-group');
//check if file size is larger than 3MB(which is 3e+6 bytes)
if (filesize > 3000000) {
alert(filesize);
//reset that input field if its file size is more than 3MB
$('[name="' + name + '"]').val('')
}
}
});
you can just include this for all input of type='file' by changing size limit in bytes.
Active X (haters gonna hate):
var filepath = 'path/to/file',
fso = new ActiveXObject('Scripting.FileSystemObject'),
file = fso.getFile(filepath),
imgbytes = file.size;
console.log(imgbytes);
Not that I know. Anyway, as client validation is only for user-friendlyness purposes, you could easilly fall back to a check on the server if your user doesn't have a browser supporting file API. With old browsers they should be used to get less user-friendly sites anyway...