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

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

none of pollyfill work for IE8 and 9 to get file size
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.

Comments

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);

2 Comments

This answer really work. But it would normally blocked becaused it would popup a warning about using Active-X. I hate this.
@JohnnyLinTW Depends on current IE settings
-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...

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.