0

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

Dev G
  • 1,387
  • 4
  • 26
  • 43
  • solution in [HTML Upload MAX_FILE_SIZE does not appear to work](http://stackoverflow.com/questions/6327965/html-upload-max-file-size-does-not-appear-to-work) – blue Apr 03 '14 at 13:39

4 Answers4

1

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

Omar Himada
  • 2,540
  • 1
  • 14
  • 31
1

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.

Shivakrishna
  • 1,333
  • 1
  • 12
  • 18
0

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);
Johan
  • 35,120
  • 54
  • 178
  • 293
-1

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...

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • Why the downvote ? is there any way of doing this using Javascript (cause this is the original question) ? I would be please to learn about it... – Laurent S. Apr 03 '14 at 13:47