snnlankrdsm
snnlankrdsm

Reputation: 1401

Image size validation

is it possible to validate file image size with jquery class orjavascript ?

Can i do that ? I made some research but did not reach anything

Thank you

Upvotes: 1

Views: 3341

Answers (2)

Maksym Kozlenko
Maksym Kozlenko

Reputation: 10363

If you want to check image file being uploaded on client side, check HTML5 File API. Here are some samples at:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

You can get file size, find it's type and access binary content.

I was using File API to read EXIF headers from image without uploading image to server.

Here is a source code:

https://gist.github.com/980275/85da4a96a3bb23bae97c3eb7ca777acdea7ed791

Upvotes: 2

coder
coder

Reputation: 13250

Try this:

<input type="file" id="loadfile" />
<input type="button" value="find size" onclick="Size()" />

Script:

 function Size() {
        if ( $.browser.msie ) {
           var a = document.getElementById('loadfile').value;
               $('#myImage').attr('src',a);
               var imgbytes = document.getElementById('myImage').fileSize;
               var imgkbytes = Math.round(parseInt(imgbytes)/1024);
               alert(imgkbytes+' KB');
        }else {
               var fileInput = $("#loadfile")[0];
               var imgbytes = fileInput.files[0].fileSize; // Size returned in bytes.
               var imgkbytes = Math.round(parseInt(imgbytes)/1024);
                       alert(imgkbytes+' KB');
         }
    }    

Upvotes: 0

Related Questions