简体   繁体   English

在javascript中查找图像的大小而不使用文件属性,因为Eclipse不支持该属性

[英]Find size of an image in javascript without using file property as it is not supported in eclipse

![enter image description here][1]I want to validate image size before submitting form via javascript. ![在此处输入图片描述] [1]我想在通过javascript提交表单之前验证图片大小。 But in eclipse: 但是在日食中:

var filesize=filename.files[0].size 

where filename is var that stores path of image is not working. 其中filename是存储图像路径的var无效。

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 有人可以帮我找到解决方法吗,或者告诉我其他方法,以便在提交表格之前在客户端检查图像大小

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_files 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM