1

![enter image description here][1]I want to validate image size before submitting form via javascript. But in eclipse:

var filesize=filename.files[0].size 

where filename is var that stores path of image is not working.

It is not supported. Can any one please help me to find solution of this or tell me any other alternative to check size of image on client side before submitting the form

1 Answers1

0

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files

<!DOCTYPE html>
<html>
<body onload="validateFileSize()">
<input type="file" id="myFile"  onchange="validateFileSize()">
<p id="demo"></p>
<script>
var maxSizeBytes=10000;
function validateFileSize(){
    var x = document.getElementById("myFile");
    var txt = "";
    if ('files' in myFile) {
        var file = x.files[0];
        if ('size' in file) {
                    if(file.size >maxSizeBytes)
                     alert("Invalid file size.Maximum allowed size is "+maxSizeBytes+" bytes");
            txt += "size: " + file.size + " bytes <br>";
         }
    }    
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>
Deshan
  • 2,112
  • 25
  • 29
  • Sir I am making this project in eclipse indigo – user3561858 Dec 16 '14 at 06:28
  • Are you checking your application in eclipse internal browser?is this a web application or mobile hybrid app? – Deshan Dec 16 '14 at 06:35
  • Yes sir I am using internal browser of eclipse. And this is a web application – user3561858 Dec 16 '14 at 06:49
  • Eclipse internal browser dosen't support file input type but this code works fine in all major desktop browsers (IE10+,Chrome all versions,FF31+,Safari 5.1 + , Opera 26+).And it would be better if you can use a real desktop browser for testing because at the end of the day your application will be running on one of them. Check this http://caniuse.com/#feat=input-file-multiple – Deshan Dec 16 '14 at 06:56